Android : Record sound in mp3 format

后端 未结 6 1406
孤城傲影
孤城傲影 2020-12-03 01:43

I am building an android app, having feature of capturing sound through microphone and playing it through headphone. For this, I have used \"AudioRecord\" and \"AudioTrack\"

相关标签:
6条回答
  • 2020-12-03 02:06

    This is work for me

    outputFile = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/myrecording.mp3";
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);`
    

    myAudioRecorder.prepare(); myAudioRecorder.start();`

    0 讨论(0)
  • 2020-12-03 02:09

    This worked for me

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/record.mp3");
    mediaRecorder.prepare();
    mediaRecorder.start();
    
    0 讨论(0)
  • 2020-12-03 02:15

    Set your outputformat of the recorder as MediaRecorder.OutputFormat.DEFAULT. I have used this and the resulting audio file is an mp3.

    0 讨论(0)
  • 2020-12-03 02:19

    Here on git you can find source code for Mp3 Voice Recorder Sample For Android .

    Checkout this source code.

    0 讨论(0)
  • 2020-12-03 02:21

    There's a work around for saving .mp3 files using MediaRecorder. Here's how:

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setOutputFile(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myrecording.mp3");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    recorder.prepare();
    recorder.start();
    

    The important part here is the setOuputFormat and the setAudioEncoder. Apparently MediaRecorder records playable mp3 if you're using MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together. Hope this helps somebody.

    Of course, if you'd rather use the AudioRecorder class I think the source code Chirag linked below should work just fine - https://github.com/yhirano/Mp3VoiceRecorderSampleForAndroid (although you might need to translate some of it from Japanese to English)

    Edit: As Bruno, and a few others pointed out in the comments, this does not encode the file in MP3. The encoding is still AAC. However, if you try to play a sound, saved using a .mp3 extension via the code above, it will still play without issues because most media players are smart enough to detect the real encoding.

    0 讨论(0)
  • 2020-12-03 02:21

    I know this is an old question. but if it will help someone I created a repository with an android java full recording app demo, the output file is an mp3 file. keep in mind, I set the target sdk to 21 if to eliminate the need for requesting user permission. this is only for the demo purposes. you can clone and use it.

    Most of the code I used is from this repo: this

    My demo repo: mp3 recording app

    0 讨论(0)
提交回复
热议问题