錄製wav音頻,由於權限導致AudioRecord無法初始化解決

首先感謝大佬完整的錄製wav音頻工程

https://www.cnblogs.com/Amandaliu/archive/2013/02/04/2891604.html

 

公司要求:

緊接着自己按照要求去實現,代碼走道如下流程的時候發生了異常:

public int startRecordAndFile() {
        //判斷是否有外部存儲設備sdcard
        if (AudioFileFunc.isSdcardExit()) {
            if (isRecord) {
                return ErrorCode.E_STATE_RECODING;
            } else {

                if (audioRecord == null) {
                    creatAudioRecord();
                }

                audioRecord.startRecording();
                // 讓錄製狀態爲true  
                isRecord = true;
                // 開啓音頻文件寫入線程  
                new Thread(new AudioRecordThread()).start();

                return ErrorCode.SUCCESS;
            }

        } else {
            return ErrorCode.E_NOSDCARD;
        }

    }

發現了這個異常

java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

後來我去網上搜的過程中,千篇一律的是設置以下權限

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

但是還是會導致無法初始化

後來我仔細去研究了一下輸出源 REMOTE_SUBMIX 

/**
         * Audio source for a submix of audio streams to be presented remotely.
         * <p>
         * An application can use this audio source to capture a mix of audio streams
         * that should be transmitted to a remote receiver such as a Wifi display.
         * While recording is active, these audio streams are redirected to the remote
         * submix instead of being played on the device speaker or headset.
         * </p><p>
         * Certain streams are excluded from the remote submix, including
         * {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_ALARM},
         * and {@link AudioManager#STREAM_NOTIFICATION}.  These streams will continue
         * to be presented locally as usual.
         * </p><p>
         * Capturing the remote submix audio requires the
         * {@link android.Manifest.permission#CAPTURE_AUDIO_OUTPUT} permission.
         * This permission is reserved for use by system components and is not available to
         * third-party applications.
         * </p>
         */
        @RequiresPermission(android.Manifest.permission.CAPTURE_AUDIO_OUTPUT)
        public static final int REMOTE_SUBMIX = 8;

後來發現需要加如下權限:

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

於是正常了,真的一個小Demo耗了我接近一下午,效率太低了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章