Android的AudioRecord初始化失败 [英] Android AudioRecord fails to initialize

查看:1442
本文介绍了Android的AudioRecord初始化失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有使用AudioRecord为Android的问题。我读过,就像我可以在网上找到它,但我似乎无法获得良好的初始化。我已经试过了Android 2.2模拟器,1.5模拟器和我的电话,HTC不可思议运行Froyo的。该仿真器和我的手机不能初始化。

8000,11025,和44100

我尝试过采样率,CHANNEL_IN_MONO /立体声和CHANNEL_CONFIGURATION_MONO /立体声,8位和16位编码(8位,使getMinBufferSize失败)和MIC的AudioSource和默认格式。所有结果变量测试运行的可得状态(初始化失败)后成为0。

这似乎从我读过的一切,这应该正确初始化的对象。我曾与buflen乘数发挥各地,有它的范围从512(函数的结果)到102400,因为我听说HTC设备需要的东西上面8192

有关测试我的问题,我做了一个新的,小的项目,如简单地再现了我的问题成为可能。我拔出需要到当地的整数然后运行构造的常量和访问的getState方法,如果它的工作检查。

 包com.example.audiorecordtest;

进口android.app.Activity;
进口android.os.Bundle;
进口android.media.AudioFormat;
进口android.media.AudioRecord;
进口android.media.MediaRecorder;

公共类audioRecordTest延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        INT频率= 8000;
        INT CHAN = AudioFormat.CHANNEL_IN_MONO;
        INT ENC = AudioFormat.ENCODING_PCM_16BIT;
        INT SRC = MediaRecorder.AudioSource.MIC;
        INT buflen = AudioRecord.getMinBufferSize(频率,成龙,ENC);
        AudioRecord AR =新AudioRecord(SRC,频率,议员,ENC,20 * buflen);
        INT测试= ar.getState();
    }
}
 

解决方案

- 编辑 -

请参阅比尔的答案。

- 编辑完 -

也许你应该检查您是否获得了正确的权限。 例如你需要得到android.permission.VIBRATE在AndroidManifest.xml文件,如果你需要震动设备。

I've been having an issue with using 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. 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. All result in the variable test becoming 0 after running a get state(failed initialization).

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.

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.

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();
    }
}

解决方案

--edit--

Please see Bill's answer.

--end edit--

Maybe you should check whether you acquired the correct permission. e.g. you need to get android.permission.VIBRATE in your AndroidManifest.xml file if you need to vibrate the device.

这篇关于Android的AudioRecord初始化失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆