AudioRecord object not initializing

元气小坏坏 提交于 2019-11-26 10:16:31
DustinB

The trick with using AudioRecord is that each device may have different initialization settings, so you will have to create a method that loops over all possible combinations of bit rates, encoding, etc.

private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    Log.d(C.TAG, "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(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e(C.TAG, rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}

AudioRecord recorder = findAudioRecord();
recorder.release();
user833970

I had the same issue, it was solved by putting

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

into the manifest.

Since Lollipop, you also need to specifically ask the user for each permission. As they may have revoked them. Make sure the permission is granted.

According to the javadocs, all devices are guaranteed to support this format (for recording):

44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT.

Change to CHANNEL_OUT_MONO for playback.

Now, with lollipop, you need to specifically ask the user for each permission. Make sure the permission is granted.

war_Hero

Even after doing all of the above steps I was getting the same issue, what worked for me was that my os was marshmallow and I had to ask for permissions.

Sirtarius

Problem with initializing few AudioRecord objects could by fixed by using audioRecord.release(); before creating next object... More here: Android AudioRecord - Won't Initialize 2nd time

Que

Just had the same problem. The solution was to restart the device. While playing with the code I did not release the AudioRecord Object which obviously caused the audio device to stuck. To test whether the audio device worked or not I downloaded Audalyzer from Google Play.

If your mobile phone system is Android M or above,perhaps you need to apply record audio permission in Android M.http://developer.android.com/guide/topics/security/permissions.html

Salman Muhammad

in my case I had to manually allow the permission in android 7 for microphone, as sean zhu commented.

I noticed that when the SDCard on the avd I am running gets full the AudioRecord constructor returns null. Have you tried clearing the SDCard?

I think this has to do with the thread not knowing that you've paused the main activity and still trying to record after you've stopped the recorder.

I solved it by changing my onResume() and onPause() methods to modify the isRecording boolean.

public void onResume() { 
    ... 
    isRecording = true;
}

public void onPause() { 
    ... 
    isRecording = false;
}

Then in your thread, surround both your startRecording() and stop() with if-statements checking for isRecording:

if(isRecording) 
    recorder.startRecording();
...
if(isRecording)
    recorder.stop(); // which you've done

I rewrote the answer from @DustinB for anyone who is using Xamarin Android AudioRecord with C#.

int[] sampleRates = new int[] { 44100, 22050, 11025, 8000 };
Encoding[] encodings = new Encoding[] { Encoding.Pcm8bit, Encoding.Pcm16bit };
ChannelIn[] channelConfigs = new ChannelIn[]{ ChannelIn.Mono, ChannelIn.Stereo };

//Not all of the formats are supported on each device
foreach (int sampleRate in sampleRates) 
{
    foreach (Encoding encoding in encodings) 
    {
        foreach (ChannelIn channelConfig in channelConfigs) 
        {
            try 
            {
                Console.WriteLine("Attempting rate " + sampleRate + "Hz, bits: " + encoding + ", channel: " + channelConfig);
                int bufferSize = AudioRecord.GetMinBufferSize(sampleRate, channelConfig, encoding);

                if (bufferSize > 0) 
                {
                    // check if we can instantiate and have a success
                    AudioRecord recorder = new AudioRecord(AudioSource.Mic, sampleRate, channelConfig, encoding, bufferSize);

                    if (recorder.State == State.Initialized)
                    {
                        mBufferSize = bufferSize;
                        mSampleRate = sampleRate;
                        mChannelConfig = channelConfig;
                        mEncoding = encoding;
                        recorder.Release();
                        recorder = null;
                        return true;
                    }
                }
            } 
            catch (Exception ex) 
            {
                Console.WriteLine(sampleRate + "Exception, keep trying." + ex.Message);
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!