4

I need to record and play an audio file, I use those two pieces of code:

For recording:

private void initRecorder() {
    try {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setOutputFile(RECORD_FILE_NAME);
        mRecorder.prepare();
        mRecorder.start();
    } catch (IllegalStateException | IOException e) {
        Utility.e("initRecorder: " + e.getMessage());
    }
}

For Playing:

    try {
        FileDescriptor fd = null;
        FileInputStream fis = new FileInputStream(RECORD_FILE_NAME);
        fd = fis.getFD();

        if (fd != null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(fd);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.prepare();

            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (mPlayTime > 0) {
                        mMediaPlayer.seekTo(mPlayTime);
                    }
                    mMediaPlayer.start();
                    setStatus(STATUS_PLAYING);
                }
            });

            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mMediaPlayer.release();
                    RecordMomentActivity.this.mPlayTime = 0;
                    setStatus(STATUS_NOT_PLAYING);
                }
            });
        }
    } catch (Exception e) {
        Utility.e("Exception: " + e.getMessage());
        setStatus(STATUS_NOT_PLAYING);
    }
}

And this code works great, records and plays audio on Nexus 5 (Android 6.0) and on LG G3 with Android 5.0. But when I use Samsung phones I get Exception:

setDataSourceFD failed.: status=0x80000000

Have someone encounter this issue? and knows how should I handle it? If not is there any alternative that I can use to record and play audio files?

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187

2 Answers2

2

This can happen if the file is corrupted or not properly handled by the MediaPlayer service that handles.

See some examples:

MediaPlayer.setDataSource causes IOException for valid file

Exception when calling setDataSource(FileDescriptor) method (failed.: status=0x80000000)

Also, can be that the file is not properly closed after recording and the player is failing.

Community
  • 1
  • 1
Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • Like I mentioned in the question, the same functions work on Nexus 5 with Android 6.0. So this is not the case of a corrupted file. But some kind of a problem with the samsung devices. – Emil Adz Oct 22 '15 at 15:50
  • As well as taken this file and played it on an other device. – Emil Adz Oct 22 '15 at 16:18
  • Take into account that some devices are a little more "resilient" to small mistakes when using APIs than others, and this is very true on media APIs. That was my point. MediaPlayer service is very prone to fail because of this kind of things. Are you stoping and releasing the media recorder after you've done recording, and before opening it for playback? Could you share this code? – Xavier Rubio Jansana Oct 22 '15 at 20:06
1

Try using the mpeg4 outputFormat

private void initRecorder() {
    try {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mRecorder.setOutputFile(RECORD_FILE_NAME);
        mRecorder.prepare();
        mRecorder.start();
    } catch (IllegalStateException | IOException e) {
        Utility.e("initRecorder: " + e.getMessage());
    }
}
NPV
  • 41
  • 4