首先感谢大佬完整的录制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耗了我接近一下午,效率太低了!