• Have Any Question?
  • contact@eitworld.com

Sound Recording





You can create a sound recorder in your own app. Just follow these simple steps and you will be able to make your own app record sound from outside world and play it.

Step 1- You will need following things to create a sound recorder and player-

  • A variable of class MediaRecorder to record the sound.
  • A variable of class File to save the recording in a file.
  • A variable of class SoundPool to play the file.
  • An integer type variable to save the ID of the recording to be played.
  • A Boolean type variable to confirm if the file has been loaded completely.
MediaRecorder media;

SoundPool pool;

boolean load=false;

File file;

int Id;

boolean load=false;

Step 2- Create objects of MediaRecorder, SoundPool and File as following-

MediaRecorder-

media=new MediaRecorder();

media.setAudioSource(MediaRecorder.AudioSource.MIC);

media.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

media.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

SoundPool-

pool= new SoundPool(10,AudioManager.STREAM_MUSIC,0);

File-

file=Environment.getExternalStorageDirectory();

file = new File(file,”filename.3gp”);

Step 3- To start the recording, set this file as the output file of MediaRecorder, prepare MediaRecorder to start and then start recording.

media.setOutputFile(file.getAbsolutePath());

try{media.prepare();}catch(Exception e){

e.printStackTrace();System.out.println(“Not Prepared”);}

media.start();

Step 4- To stop the recording, use-

media.stop();

Step 5- Now it is time to play the file. To play the file, SoundPool is used. First load the SoundPool with the file created above and then check if the file has been successfully loaded or not.

Id=pool.load(file.getAbsolutePath(),1);

pool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

@Override

public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {

load=true;

}

});

Step 6- Check if the loading has been completed. If ‘load’ becomes true that means loading is completed. Set the volume level for the pool and get it started.

if(load){

AudioManager ad=(AudioManager)getSystemService(AUDIO_SERVICE);

Float vol=(float)ad.getStreamVolume(AudioManager.STREAM_MUSIC);

Float mvol=(float)ad.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

Float volume=vol/mvol;

pool.play(Id, volume, volume, 1, 0, 1f);}

Step 7- Following are other options for SoundPool-

  • To pause the playback-

    pool.pause(Id);

  • To resume the playback-

    pool.resume(Id);

  • To stop the playback-

    pool.stop(Id);

Step 8- Remember to release both MediaRecorder and SoundPool before ending the application. onStop() method of class Activity can be used for this.

@Override

protected void onStop() {

// TODO Auto-generated method stub

super.onStop();

if(pool!=null){

pool.stop(sId);

pool.release();

media.release();

}

super.onStop();

}

Step 9- Give permissions for writing in external memory and recording sound in AndroidManifest.xml file.

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name=”android.permission.RECORD_AUDIO”/>



Real Time Web Analytics