Android AudioRecord错误启动状态-38

时间:2018-05-22 16:59:28

标签: android audio-recording android-audiorecord

我正面临这个错误,试图用音频记录开始录制会话,似乎是音频源很忙,但我找不到任何有关此错误代码的文档或信息。我想做的是

private class AudioRecordRunnable implements Runnable {

    private final AudioRecord mAudioRecord;
    private final AudioDataCallback mAudioDataCallback;

    private byte[] mByteBuffer;
    private final short[] mShortBuffer;
    private final int mByteBufferSize;
    private final int mShortBufferSize;
    private final int mAudioFormat;

    AudioRecordRunnable(int sampleRate, int channelConfig, int audioFormat, int byteBufferSize,
                        @NonNull AudioDataCallback audioDataCallback) {
        mAudioFormat = audioFormat;
        int minBufferSize =
                AudioRecord.getMinBufferSize(sampleRate, channelConfig, mAudioFormat);
        mByteBufferSize = byteBufferSize;
        mShortBufferSize = mByteBufferSize / 2;

        mShortBuffer = new short[mShortBufferSize];
        bufferSize = Math.max(minBufferSize, byteBufferSize);
        mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig,
                audioFormat, bufferSize);
        mAudioDataCallback = audioDataCallback;
    }

    @Override
    public void run() {
        if (mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
            try {
                try {
                    mAudioRecord.startRecording();
                } catch (IllegalStateException e) {
                    Log.w(TAG, "startRecording fail: " + e.getMessage());
                    mAudioDataCallback.onError();
                    return;
                }
                while (mIsRecording.get()) {
                    int ret;
                    if (mAudioFormat == AudioFormat.ENCODING_PCM_16BIT) {
                        ret = mAudioRecord.read(mShortBuffer, 0, mShortBufferSize);
                        if (ret > 0) {
                            mByteBuffer = new byte[mByteBufferSize];
                            mAudioDataCallback.onAudioData(
                                    short2byte(mShortBuffer, ret, mByteBuffer), ret * 2);
                        } else {
                            onError(ret);
                            break;
                        }
                    } else {
                        ret = mAudioRecord.read(mByteBuffer, 0, mByteBufferSize);
                        if (ret > 0) {
                            mAudioDataCallback.onAudioData(mByteBuffer, ret);
                        } else {
                            onError(ret);
                            break;
                        }
                    }
                }

            }finally {
                mAudioRecord.stop();
                mAudioRecord.release();
            }
        }
    }


    private byte[] short2byte(short[] sData, int size, byte[] bData) {
        if (size > sData.length || size * 2 > bData.length) {
            Log.w(TAG, "short2byte: too long short data array");
        }
        for (int i = 0; i < size; i++) {
            bData[i * 2] = (byte) (sData[i] & 0x00FF);
            bData[(i * 2) + 1] = (byte) (sData[i] >> 8);
        }
        return bData;
    }

    private void onError(int errorCode) {
        mAudioDataCallback.onError();
        Log.w(TAG, "record fail: " + String.valueOf(errorCode));
        if (errorCode == AudioRecord.ERROR_INVALID_OPERATION) {
            Log.w(TAG, "record fail: ERROR_INVALID_OPERATION");

        } else if (errorCode == AudioRecord.ERROR_BAD_VALUE) {
            Log.w(TAG, "record fail: ERROR_BAD_VALUE");
        }
    }
}

我在logcat中收到此消息

E / AudioRecord:start()状态-38

但是没有抛出任何异常和结果     ret = mAudioRecord.read(mShortBuffer,0,mShortBufferSize);

始终为0

如何避免此错误?

1 个答案:

答案 0 :(得分:0)

也许,另一个过程正在使用您的麦克风。