very poor quality of audio recorded on my droidx using MediaRecorder, why?

前端 未结 5 1871
予麋鹿
予麋鹿 2020-12-09 22:48

my project requires me to be able to record audio on an android device. i implemented solution using the MediaRecorder() but the recorded audio is in a terrible quality. wha

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 23:16

    The big problem I had in my audio recording quality was that I set the bit rate way too low at 16. 16 is a value that generally corresponds to bit depth, not bit rate. Bit rate is usually listed as kbps, but MediaRecorder.setAudioEncodingBitRate() take bps (note no "k"). Try this:

    MediaRecorder recorder = new MediaRecorder();
    
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(...);
    recorder.setAudioEncoder(...);
    
    final int bitDepth = 16;
    final int sampleRate = 44100;
    final int bitRate = sampleRate * bitDepth;
    
    recorder.setAudioEncodingBitRate(bitRate);
    recorder.setAudioSamplingRate(sampleRate);
    
    recorder.setOutputFile(...);
    
    recorder.prepare();
    recorder.start();
    // stop
    recorder.stop();
    recorder.reset(); 
    recorder.release();
    

    You'll need to check supported media formats to figure out the correct matchings of format (audio encoder), container (output format), sampling rate (usually given in kHz but passed to MediaRecorder.setSamplingRate() as just Hz), and bit rate (usually given in kbps). Note that not all formats in the documentation have specified bit rates; using the method in the code above should give a fair guess at the proper bit rate in that case.

    Thanks to @StarPinkER, @PrvN, @arlomedia in this SO answer for helping me clear this up.

提交回复
热议问题