【问题标题】:Android AudioRecord fails to initialise(Other solutions not working)Android AudioRecord 无法初始化(其他解决方案不起作用)
【发布时间】:2016-06-29 23:58:24
【问题描述】:

我今天检查并尝试了所有其他线程几个小时,但没有一个解决方案有效。

我已尝试过滤所有可用的音频选项。我已授予该应用适当的权限。

目标:我正在尝试获取此音频流,以便获取音频的频率。

我的东西

public int audioSource = MediaRecorder.AudioSource.MIC;
public int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public AudioRecord audioRecord = null;
private Thread recordingThread = null;
public int blockSize = 256;                               // deal with this many samples at a time
public int sampleRate = 8000;                             // Sample rate in Hz

稍后...

 int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding);
    AudioRecord audioeeeeRecord = new AudioRecord(audioSource, sampleRate, channelConfig, audioEncoding, bufferSize);   // The RAW PCM sample recording
    audioRecord = audioeeeeRecord;
    if (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED)
        try {
            throw new Exception("AudioRecord init failed");                    //audioRecord = findAudioRecord();
        } catch (Exception e) {
            e.printStackTrace();
        }
    final short[] buffer = new short[blockSize];

    try {

        audioRecord.startRecording();
    } catch (Exception e) {
        e.printStackTrace();
    }

这会引发以下错误 (http://pastebin.com/raw/3wmA4cku):

AudioRecord: AudioFlinger could not create record track, status: -1
         E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
         E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
         W/System.err: java.lang.Exception: AudioRecord init failed
         W/System.err:     at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:102)
         W/System.err:     at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
         W/System.err:     at android.view.View.performClick(View.java:5697)
         W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
         W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
         W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
         W/System.err:     at android.os.Looper.loop(Looper.java:158)
         W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
         W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
         W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
         W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
         W/System.err: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
         W/System.err:     at android.media.AudioRecord.startRecording(AudioRecord.java:943)
         W/System.err:     at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:114)
         W/System.err:     at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
         W/System.err:     at android.view.View.performClick(View.java:5697)
         W/System.err:     at android.view.View$PerformClick.run(View.java:22526)
         W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
         W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
         W/System.err:     at android.os.Looper.loop(Looper.java:158)
         W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
         W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
         W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
         W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

【问题讨论】:

  • 你能粘贴完整的源代码吗?您是否在另一个线程中运行记录循环?你的安卓版本是多少?

标签: java android android-audiorecord


【解决方案1】:

第 1 步)检查您的清单以确保在应用程序部分添加 RECORD_AUDIO 权限。例如

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.colibri.audioloopback">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

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

</manifest>

步骤 2) 如果您的设备是 M 或更高版本,您需要从 Settings->App->Your App->Permissions 授予权限。

第三步)如果还是不行,参考下面的代码sn-p。

   public void audioRecordLoop() throws Exception {
        Log.e(TAG,"start audioRecordLoop");
        int channelConfig = mChannels == 2?
                AudioFormat.CHANNEL_IN_STEREO:
                AudioFormat.CHANNEL_IN_MONO;
        int bufferSize = AudioRecord.getMinBufferSize(
                mSampleRateHz, channelConfig, mAudioEncoding);
        mAudioRecord = new AudioRecord(
                mAudioSource, mSampleRateHz, channelConfig, mAudioEncoding, bufferSize);// The RAW PCM sample recording

        if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
            Log.e(TAG,"AudioRecord init failed");
            return;
        }
        final short[] buffer = new short[mBlockSize];
        mAudioRecord.startRecording();
        int len = 0;
        while (mbExit == false) {
            len = mAudioRecord.read(buffer, 0, mBlockSize);
            if (len < 0) {
                Log.e(TAG,"read error " + len);
                return;
            }
        }
        mAudioRecord.stop();
        mAudioRecord.release();
    }

它现在应该可以工作了!

【讨论】:

    【解决方案2】:

    我认为问题可能是您在之前的尝试中绑定了音频资源,然后得到了正确的代码,但现在由于没有可用的音频资源而无法正常工作。见this SO question。此外,当构造函数返回未初始化的 AudioRecord 时,您会抛出异常,但您会捕获自己的异常,这会导致日志中出现第二个异常(...调用未初始化的 AudioRecord ...),可以忽略它第一个结果。

    您可能需要在 Activity 中放置一些 try/finally 部分和/或 OnDestroy 和/或 OnPause 方法,以确保在不再使用时释放 audioRecord。

    一旦您有了清理代码,您可能需要关闭您的应用和/或重新启动您的手机以释放音频资源。

    当然,您有可能让另一个正在运行的应用程序拿着麦克风,但我对此表示怀疑。

    【讨论】:

    • 我刚刚实现了 finally catch。我已经重启了我的手机。我试过用模拟器。每次收到java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. 的错误时,请先尝试
    • @Chris 你不应该依赖模拟器来解决棘手的问题,绝对不是。
    【解决方案3】:

    这就是答案。这不是android有的东西,这很愚蠢。

    https://stackoverflow.com/a/33769527/6530367 I know this is a very old question but in Android Marshmallow you have to go on "Settings &gt; Apps &gt; Your App &gt; Permissions" and enble Microphone permission.

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多