Search code examples
androidaudiomediaandroid-mediacodecandroid-mediarecorder

Audio file captured by MediaRecorder is broken after it is sent to server


My app records an audio clip and send the clip to the server using Retrofit2 after the recording is completed.The file is received in server,but the file is broken,what I mean by broken is that it cannot be played. I use the following URL(example /audio/myaudio.mp4) to play the audio clip,which I tried with another audio file using postman,the audio file can be played successfully.Besides,even downloading the audio clip captured by android via Filezilla also has the same broken file.

This is how I record the audio:

mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncodingBitRate(32000);
        mRecorder.setAudioSamplingRate(44100);

 File root = android.os.Environment.getExternalStorageDirectory();
    File file = new File(root.getAbsolutePath() + "/Audios");
    if (!file.exists()) {
        file.mkdirs();
    }

    outputFile =  root.getAbsolutePath() + "/Audios/" + String.valueOf(System.currentTimeMillis() + ".mp3");
    Log.d("filename",outputFile);
    mRecorder.setOutputFile(outputFile);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

I have also tried to change the file extension to .mp3 , .mp4 and .3gpp as well,but none of them seem to work on the server side. To make things clear, I've attach the code which I used to send the audio file to server using Retrofit2:

Same questions have asked by some one else. I tried those solutions but it also not working. Can anyone help me .

question : Audio file captured by MediaRecorder is broken after it is sent to server using Retrofit 2


Solution

  • AAC format worked for me

    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);