Question

I want the audio file created during audio capture to be stored in the internal memory and played back from there. I am getting media player prepare failed() when trying to do so

Was it helpful?

Solution

Here's an example of how to do this using MediaRecorder. First add the correct permissions (you will need to add more if you choose to use external storage).

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

This records the audio in internal storage:

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilesDir()+"/audio.m4a");
recorder.prepare();
recorder.start(); 
...
recorder.stop();
recorder.reset();
recorder.release()

This plays the audio from internal storage:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getFilesDir()+"/audio.m4a");
mediaPlayer.prepare();
mediaPlayer.start();

getFilesDir() gives you the path to your internal files. Here's a link to more information on data storage, http://developer.android.com/guide/topics/data/data-storage.html#filesInternal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top