AudioRecord录音初始化异常

蛋疼一上午... 之前用MediaRecorder 录音,保存和播放都写好了,测试没问题,然后需求需要录音和音乐合成功能,完蛋,项目需要加录音和音乐合成功能,MediaRecorder 录制了音频没法处理, 只能换成AudioRecord;

啪啪啪,写好代码,启动测试,各种报异常startRecord(),called on an uninitialized AudioRecord.  看这信息说 没有初始化AudioRecord就开始录音了;整个录音逻辑 我是写在一个类里的,没有写到activity中,初始化代码放在构造函数里,初始化方法应该是走了的;打日志测试,诚然如此,初始化代码有走到;

那就应该是初始化有问题了:初始化代码如下:

int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
				AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz,
						AudioFormat.CHANNEL_IN_MONO,
						AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
标标准准的AudioRecord初始化,然后给这段代码加上try catch:

if (mAudioRecord == null) {
			try {
				mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
						sampleRateInHz, AudioFormat.CHANNEL_IN_MONO,
						AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				LogUtil.e("", "initRecorder():exception");
				e.printStackTrace();
			}}

打印之, 日志看到Could not get audio input for record source 1;看来是参数有问题了, 看看构造函数说明:

Parameters:
audioSource the recording source. See MediaRecorder.AudioSource for recording source definitions.
sampleRateInHz the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices.
channelConfig describes the configuration of the audio channels. See AudioFormat.CHANNEL_IN_MONO and AudioFormat.CHANNEL_IN_STEREO. AudioFormat.CHANNEL_IN_MONO is guaranteed to work on all devices.
audioFormat the format in which the audio data is represented. See AudioFormat.ENCODING_PCM_16BIT and AudioFormat.ENCODING_PCM_8BIT
bufferSizeInBytes the total size (in bytes) of the buffer where audio data is written to during the recording. New audio data can be read from this buffer in smaller chunks than this size. See getMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioRecord instance. Using values smaller than getMinBufferSize() will result in an initialization failure.
 都没错,那换下参数值试试,换采样率大小,换声道,换编码率...  忙活一上午,结果都都是Could not get audio input for record source 1!!
难道是手机硬件兼容性的问题?我的手机是魅族MX ,除了分辨率奇葩点 其他都还算正常,应该不会有什么特别的地方; 然后继续百度. 最后在一个博客中看到 他用M9 也遇到同样的问题,搞了3天,最后重启下手机就OK 了....
死马当活马医,重启手机...
开机后代码原封不动,重新跑下程序,居然真的一切顺畅,录音能正常工作, 打开之前设置的录音保存地址, 录音文件也有了! 整个过程居然毫无违和感....
蛋疼,遂记之,继续码代码...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值