开发者

Recording in Android giving exceptions

When I am trying to record an audio through the emulator using following code

  AudioRecord recordInstance = new AudioRecord(
    MediaRecorder.AudioSource.MIC, this.getFrequency(), this
      .getChannelConfiguration(), this.getAudioEncoding(开发者_运维知识库),
    bufferSize);

Then I am getting following exceptions in logcat:

12-16 19:07:31.680: INFO/jdwp(223): Ignoring second debugger -- accepting and dropping
12-16 19:07:31.700: ERROR/AudioHardware(34): Error opening input channel
12-16 19:07:31.720: WARN/AudioHardwareInterface(34): getInputBufferSize bad sampling rate: 11025
12-16 19:07:31.730: ERROR/AudioRecord(294): Recording parameters are not supported: sampleRate 11025, channelCount 1, format 1
12-16 19:07:31.730: ERROR/AudioRecord-JNI(294): Error creating AudioRecord instance: initialization check failed.

12-16 19:07:31.730: ERROR/AudioRecord-Java(294): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
12-16 19:07:31.730: WARN/dalvikvm(294): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
12-16 19:07:31.770: ERROR/AndroidRuntime(294): FATAL EXCEPTION: Thread-8
12-16 19:07:31.770: ERROR/AndroidRuntime(294): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at android.media.AudioRecord.startRecording(AudioRecord.java:495)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at com.prospeak.Recorder.run(Recorder.java:84)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at java.lang.Thread.run(Thread.java:1096)

Can you figure out what's wrong in this code?


your sample rate is wrong, try 8000Hz. It is an emulator limitation.


Also, make sure that you have this permission set in your AndroidManifest.xml: <uses-permission android:name="android.permission.RECORD_AUDIO" />


If you haven't registered the permission to record audio in your manifest then you will get an error.


The error is:

getInputBufferSize bad sampling rate: 11025

You need a different sample rate. You could loop through potential sample rates until you don't have an Exception.

As far as trying to record from the Emulator, it says on the Android Developer site that it's not possible so it would probably be best to test from a phone or to use a pre-recorded sample.


guys audio record only work on,

    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, 500000);
    recorder.startRecording();

i search it for long time and last found that, but it only produces noise, try to get good quality audio


Make sure that you check that the audiorecord.getState() == initialized.

Sample code in that linked post:

https://stackoverflow.com/questions/tagged/sample-rate+android


I struggled with a similar issue recently. As said earlier you have to cycle through all possible combinations of Sample rates, Channel configurations, Audio Formats to know what fits for a given phone. The emulator AFAIK, does not support audio input and will always give an initialization error on AudioRecord objects. So you have to test your code on a real device. But additionally, I discovered that there is a maximum limit to the buffer size you can give for the AudioRecord object. It must be somewhere in a few KB's. I tried to set it at about 10 MB and it kept giving the same initialization error. So be careful. Here is the code to cycle through the Formats and Sample rates(courtesy somewhere on StackEx):

 private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
private static short [] aformats = new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT };
private static short [] chConfigs = new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO };


public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : aformats) {
            for (short channelConfig : chConfigs) {
                try {
                    Log.d("Log:", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC, rate, channelConfig, audioFormat, java.lang.Math.max(bufferSize,1024*800));

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
                            Chosen_SR = rate;
                            audFormat = audioFormat;
                            chanConfig = channelConfig;
                            return recorder;
                        }

                    }
                } catch (Exception e) {
                    Log.e("Log:", rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}
AudioRecorder mRecord = findAudioRecord();

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜