Android Interview Questions - SPLessons

Android MediaRecorder

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android MediaRecorder

Android MediaRecorder

Android provides you an option of capturing audio and store it in your phone. Though there are many ways to do this, MediaRecorder class is the most common way to capture audio or video. In order to use the MediaRecorder class, you will have to first create an instance of the MediaRecorder class. The following is the syntax to create an instance:
MediaRecorder recorder = new MediaRecorder();
You can also set the source, output, encoding format, and output file.
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
recorder.setOutputFile(file);
You can call the prepare() and start() methods to start recording an audio.
recorder.prepare();
recorder.start();
MediaRecorder class also provides stop() and release() methods to stop recording and release recording instance respectively.
recorder.stop();
recorder.release();
The following example illustrates how to capture an audio and play a recorded audio. Create MainActivity.java under src/<your packagename>. MainActivity.java: [java] public class MainActivity extends Activity { private MediaRecorder recorder; private MediaPlayer player; private String file = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set activity content to external view setContentView(R.layout.activity_main); //find views by Id Button stop2=(Button)findViewById(R.id.button14); Button play=(Button)findViewById(R.id.button13); Button stop1=(Button)findViewById(R.id.button12); Button record=(Button)findViewById(R.id.button11); // store it to sd card file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/splessons.3gpp"; recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); recorder.setOutputFile(file); //on click record record.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "Recording started",Toast.LENGTH_SHORT).show(); } }); //on click stop recording stop1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { recorder.stop(); recorder.release(); Toast.makeText(getApplicationContext(), "Recording Stopped",Toast.LENGTH_SHORT).show(); } catch(Exception e) { e.printStackTrace(); } } }); //on click play play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub player = new MediaPlayer(); try { player.setDataSource(file); player.prepare(); player.start(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); //on click stop playing stop2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { if (player != null) { player.stop(); player.release(); player = null; Toast.makeText(getApplicationContext(), "Player Stopped",Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } } [/java] Create activity_main.xml under res/layout folder. activity_main.xml: [xml]