录制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耗了我接近一下午,效率太低了!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值