Search code examples
androidandroid-mediaplayervoice-recording

Android - MediaRecorder - often producing unplayable Output


My app uses the MediaRecorder for recording a voice message. In about 50% of the situations a file is produced that cannot be played with the MediaPlayer. When I use the VLC player, all audio is ok.

I know that the sequence of the MediaRecorder states is important. I also tried whether being online is important or not. No difference.

Can changing the format / codec help? Why can I play the file in VLC and not the MediaPlayer?

What do I do:

mRecorder = new MediaRecorder();
mRecorder.reset();
mRecorder.setAudioSource( MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder( MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile( audioFileName);
try {
    mRecorder.prepare();
    mRecorder.start();
} catch ( Exception e) {
    e.printStackTrace();
}
saveSpeech();

Method saveSpeech is just another method in the same file. I placed the 'reset' later on - still no difference.

void saveSpeech( final Waypoint wp) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder( WaypointActivity.thisActivity);
    alertDialog.setTitle( "Recording ...");
    alertDialog.setMessage( "Recording ... ");
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            if( mRecorder != null) { 
                mRecorder.stop();
                mRecorder.reset();
                mRecorder.release();
                mRecorder = null; 
            }
            if( audioFileName == null) {
                UserFeedback.show( "Lost file name ... oops, App error"); 
                return ; 
            }
        }
    });
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });
    alertDialog.show();
}

Example of a valid 3GP file. Example of an invalid 3GP file.

On request, the stack trace added:

Exception: message=[Prepare failed.: status=0x1], cause=[null], class=[class java.io.IOException], trace=[java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1158)
at nl.deholtmans.waypoints.FragmentWpDetailUpdate$4.onItemClick(FragmentWpDetailUpdate.java:262)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Solution

  • Try changing your AudioEncoder for AAC. As most Media Players are able to play this type of file.