문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top