<span style="color: #993300;">/*</span>
<span style="color: #993300;"> * The application needs to have the permission to write to external storage</span>
<span style="color: #993300;"> * if the output file is written to the external storage, and also the</span>
<span style="color: #993300;"> * permission to record audio. These permissions must be set in the</span>
<span style="color: #993300;"> * application's AndroidManifest.xml file, with something like:</span>
<span style="color: #993300;"> *</span>
<span style="color: #993300;"> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></span>
<span style="color: #993300;"> * <uses-permission android:name="android.permission.RECORD_AUDIO" /></span>
<span style="color: #993300;"> *</span>
<span style="color: #993300;"> */</span>
<span style="color: #000000;"><span style="color: #000080;">package</span> com.android.audiorecordtest;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.app.<span style="color: #800080;">Activity</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.widget.<span style="color: #800080;">LinearLayout</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.os.<span style="color: #800080;">Bundle</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.os.<span style="color: #800080;">Environment</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.view.<span style="color: #800080;">ViewGroup</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.widget.<span style="color: #800080;">Button</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.view.<span style="color: #800080;">View</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.view.View.<span style="color: #800080;">OnClickListener</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.content.<span style="color: #800080;">Context</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.util.<span style="color: #800080;">Log</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.media.<span style="color: #800080;">MediaRecorder</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> android.media.<span style="color: #800080;">MediaPlayer</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">import</span> java.io.<span style="color: #800080;">IOException</span>;</span>
<span style="color: #000000;"><span style="color: #000080;">public</span> <span style="color: #000080;">class</span> <span style="color: #800080;">AudioRecordTest</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Activity</span></span>
<span style="color: #000000;">{</span>
<span style="color: #000000;"><span style="color: #000080;"> private static</span> <span style="color: #000080;">final</span> <span style="color: #800080;">String</span> LOG_TAG = "AudioRecordTest";</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">static</span> <span style="color: #800080;">String</span> mFileName = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">RecordButton</span> mRecordButton = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">MediaRecorder</span> mRecorder = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">PlayButton </span> mPlayButton = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">MediaPlayer </span> mPlayer = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> onRecord(<span style="color: #000080;">boolean</span> start) {</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (start) {</span>
<span style="color: #000000;"> startRecording();</span>
<span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span>
<span style="color: #000000;"> stopRecording();</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> onPlay(<span style="color: #000080;">boolean</span> start) {</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (start) {</span>
<span style="color: #000000;"> startPlaying();</span>
<span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span>
<span style="color: #000000;"> stopPlaying();</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> startPlaying() {</span>
<span style="color: #000000;"> mPlayer = <span style="color: #000080;">new</span> <span style="color: #800080;">MediaPlayer</span>();</span>
<span style="color: #000000;"><span style="color: #000080;"> try</span> {</span>
<span style="color: #000000;"> mPlayer.setDataSource(mFileName);</span>
<span style="color: #000000;"> mPlayer.prepare();</span>
<span style="color: #000000;"> mPlayer.start();</span>
<span style="color: #000000;"> } <span style="color: #000080;">catch</span> (<span style="color: #800080;">IOException</span> e) {</span>
<span style="color: #000000;"><span style="color: #800080;"> Log</span>.e(LOG_TAG, <span style="color: #008000;">"prepare() failed"</span>);</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> stopPlaying() {</span>
<span style="color: #000000;"> mPlayer.release();</span>
<span style="color: #000000;"> mPlayer = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> startRecording() {</span>
<span style="color: #000000;"> mRecorder = <span style="color: #000080;">new</span> <span style="color: #800080;">MediaRecorder</span>();</span>
<span style="color: #000000;"> mRecorder.setAudioSource(<span style="color: #800080;">MediaRecorder.AudioSource</span>.MIC);</span>
<span style="color: #000000;"> mRecorder.setOutputFormat(<span style="color: #800080;">MediaRecorder.OutputFormat</span>.THREE_GPP);</span>
<span style="color: #000000;"> mRecorder.setOutputFile(mFileName);</span>
<span style="color: #000000;"> mRecorder.setAudioEncoder(<span style="color: #800080;">MediaRecorder.AudioEncoder</span>.AMR_NB);</span>
<span style="color: #000000;"><span style="color: #000080;"> try</span> {</span>
<span style="color: #000000;"> mRecorder.prepare();</span>
<span style="color: #000000;"> } <span style="color: #000080;">catch</span> (<span style="color: #800080;">IOException</span> e) {</span>
<span style="color: #000000;"><span style="color: #800080;"> Log</span>.e(LOG_TAG, <span style="color: #008000;">"prepare() failed"</span>);</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> mRecorder.start();</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> stopRecording() {</span>
<span style="color: #000000;"> mRecorder.stop();</span>
<span style="color: #000000;"> mRecorder.release();</span>
<span style="color: #000000;"> mRecorder = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> class</span> <span style="color: #800080;">RecordButton</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Button</span> {</span>
<span style="color: #000000;"><span style="color: #000080;"> boolean</span> mStartRecording = <span style="color: #000080;">true</span>;</span>
<span style="color: #000000;"><span style="color: #800080;"> OnClickListener</span> clicker = <span style="color: #000080;">new</span> <span style="color: #800080;">OnClickListener</span>() {</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onClick(<span style="color: #800080;">View</span> v) {</span>
<span style="color: #000000;"> onRecord(mStartRecording);</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (mStartRecording) {</span>
<span style="color: #000000;"> setText("Stop recording");</span>
<span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span>
<span style="color: #000000;"> setText("Start recording");</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> mStartRecording = !mStartRecording;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> };</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">RecordButton</span>(<span style="color: #800080;">Context</span> ctx) {</span>
<span style="color: #000000;"><span style="color: #000080;"> super</span>(ctx);</span>
<span style="color: #000000;"> setText(<span style="color: #008000;">"Start recording"</span>);</span>
<span style="color: #000000;"> setOnClickListener(clicker);</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> class</span> <span style="color: #800080;">PlayButton</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Button</span> {</span>
<span style="color: #000000;"><span style="color: #000080;"> boolean</span> mStartPlaying = <span style="color: #000080;">true</span>;</span>
<span style="color: #000000;"><span style="color: #800080;"> OnClickListener</span> clicker = <span style="color: #000080;">new</span> <span style="color: #800080;">OnClickListener</span>() {</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onClick(<span style="color: #800080;">View</span> v) {</span>
<span style="color: #000000;"> onPlay(mStartPlaying);</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (mStartPlaying) {</span>
<span style="color: #000000;"> setText(<span style="color: #008000;">"Stop playing"</span>);</span>
<span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span>
<span style="color: #000000;"> setText(<span style="color: #008000;">"Start playing"</span>);</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> mStartPlaying = !mStartPlaying;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> };</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">PlayButton</span>(<span style="color: #800080;">Context</span> ctx) {</span>
<span style="color: #000000;"><span style="color: #000080;"> super</span>(ctx);</span>
<span style="color: #000000;"> setText(<span style="color: #008000;">"Start playing"</span>);</span>
<span style="color: #000000;"> setOnClickListener(clicker);</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">AudioRecordTest</span>() {</span>
<span style="color: #000000;"> mFileName = <span style="color: #800080;">Environment</span>.getExternalStorageDirectory().getAbsolutePath();</span>
<span style="color: #000000;"> mFileName += <span style="color: #008000;">"/audiorecordtest.3gp"</span>;</span>
<span style="color: #000000;"> }</span>
<span style="color: #008080;"> @Override</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onCreate(<span style="color: #800080;">Bundle</span> icicle) {</span>
<span style="color: #000000;"><span style="color: #000080;"> super</span>.onCreate(icicle);</span>
<span style="color: #000000;"><span style="color: #800080;"> LinearLayout</span> ll = <span style="color: #000080;">new</span> <span style="color: #800080;">LinearLayout</span>(<span style="color: #000080;">this</span>);</span>
<span style="color: #000000;"> mRecordButton = <span style="color: #000080;">new</span> <span style="color: #800080;">RecordButton</span>(<span style="color: #000080;">this</span>);</span>
<span style="color: #000000;"> ll.addView(mRecordButton,</span>
<span style="color: #000000;"><span style="color: #000080;"> new</span> <span style="color: #800080;">LinearLayout.LayoutParams</span>(</span>
<span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span>
<span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span>
<span style="color: #000000;"><span style="color: #008080;"> 0</span>));</span>
<span style="color: #000000;"> mPlayButton = <span style="color: #000080;">new</span> <span style="color: #800080;">PlayButton</span>(<span style="color: #000080;">this</span>);</span>
<span style="color: #000000;"> ll.addView(mPlayButton,</span>
<span style="color: #000000;"><span style="color: #000080;"> new</span> <span style="color: #800080;">LinearLayout.LayoutParams</span>(</span>
<span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span>
<span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span>
<span style="color: #000000;"><span style="color: #008080;"> 0</span>));</span>
<span style="color: #000000;"> setContentView(ll);</span>
<span style="color: #000000;"> }</span>
<span style="color: #008080;"> @Override</span>
<span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onPause() {</span>
<span style="color: #000000;"><span style="color: #000080;"> super</span>.onPause();</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (mRecorder != <span style="color: #000080;">null</span>) {</span>
<span style="color: #000000;"> mRecorder.release();</span>
<span style="color: #000000;"> mRecorder = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"><span style="color: #000080;"> if</span> (mPlayer != <span style="color: #000080;">null</span>) {</span>
<span style="color: #000000;"> mPlayer.release();</span>
<span style="color: #000000;"> mPlayer = <span style="color: #000080;">null</span>;</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;"> }</span>
<span style="color: #000000;">}</span>