简体   繁体   English

Android AudioRecord无法初始化

[英]Android AudioRecord fails to initialize

I've been having an issue with using AudioRecord for Android. 我在使用AudioRecord for Android时遇到了问题。 I've read as much as I can find online about it, but I cannot seem to get a good initialization. 我已经阅读了尽可能多的在线信息,但我似乎无法获得良好的初始化。 I have tried the Android 2.2 emulator, 1.5 emulator and my phone, an HTC Incredible running Froyo. 我已经尝试过Android 2.2模拟器,1.5模拟器和我的手机,HTC Incredible运行Froyo。 The emulators and my phone fail initialization. 模拟器和我的手机初始化失败。

I've tried sampling rates of 8000, 11025, and 44100, formats of CHANNEL_IN_MONO/STEREO and CHANNEL_CONFIGURATION_MONO/STEREO, 8bit and 16bit encoding (8 bit makes the getMinBufferSize fail), and AudioSource of MIC and DEFAULT. 我尝试过采样率为8000,11025和44100,格式为CHANNEL_IN_MONO / STEREO和CHANNEL_CONFIGURATION_MONO / STEREO,8位和16位编码(8位使得getMinBufferSize失败),以及MIC和DEFAULT的AudioSource。 All result in the variable test becoming 0 after running a get state(failed initialization). 所有这些都导致变量测试在运行get状态(初始化失败)后变为0。

It seems from everything I've read that this should correctly initialize the object. 从我读过的所有内容看来,这应该正确地初始化对象。 I have played around with the multiplier on buflen, to have it range from 512 (the result of the function) to 102400 because I had heard that HTC devices require something above 8192. 我已经玩过buflen的乘数,它的范围从512(函数的结果)到102400,因为我听说过HTC设备需要8192以上的东西。

For testing my problem I made a new, small project that recreates my problem as simply as possible. 为了测试我的问题,我创建了一个新的小项目,尽可能简单地重现我的问题。 I pull out the constants needed into local ints then run the constructor and access the getState method for checking if it worked. 我将所需的常量拉出到本地int中,然后运行构造函数并访问getState方法以检查它是否有效。

package com.example.audiorecordtest;

import android.app.Activity;
import android.os.Bundle;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

public class audioRecordTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int freq =8000;
        int chan = AudioFormat.CHANNEL_IN_MONO;
        int enc  = AudioFormat.ENCODING_PCM_16BIT;
        int src  = MediaRecorder.AudioSource.MIC;
        int buflen = AudioRecord.getMinBufferSize(freq, chan, enc);
        AudioRecord ar = new AudioRecord(src,freq,chan,enc,20*buflen);
        int test = ar.getState();
    }
}

I think he means you need the RECORD_AUDIO permission in the manifest: 我认为他意味着你需要清单中的RECORD_AUDIO权限:

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

That worked for me. 这对我有用。

--edit-- - 编辑 -

Please see Bill's answer. 请看比尔的回答。

--end edit-- - 编辑 -

Maybe you should check whether you acquired the correct permission. 也许你应该检查一下你是否获得了正确的许可。 eg you need to get android.permission.VIBRATE in your AndroidManifest.xml file if you need to vibrate the device. 例如,如果需要振动设备,则需要在AndroidManifest.xml文件中获取android.permission.VIBRATE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM