Android AudioRecord 初始化失败

发布于 2024-10-02 13:50:11 字数 1365 浏览 8 评论 0原文

我在使用 Android 版 AudioRecord 时遇到问题。我已经在网上阅读了尽可能多的内容,但我似乎无法获得良好的初始化。我尝试过 Android 2.2 模拟器、1.5 模拟器和我的手机(运行 Froyo 的 HTC Incredible)。模拟器和我的手机初始化失败。

我尝试过8000、11025和44100的采样率,CHANNEL_IN_MONO/STEREO和CHANNEL_CONFIGURATION_MONO/STEREO的格式,8位和16位编码(8位使getMinBufferSize失败),以及MIC和DEFAULT的AudioSource。所有这些都会导致变量 test 在运行 get state 后变为 0(初始化失败)。

从我读到的所有内容看来,这应该正确初始化对象。我在 buflen 上尝试过乘数,使其范围从 512(函数的结果)到 102400,因为我听说 HTC 设备需要高于 8192 的值。

为了测试我的问题,我创建了一个新的小项目来重新创建我的问题尽可能简单。我将所需的常量提取到本地整数中,然后运行构造函数并访问 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'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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以为你会在 2024-10-09 13:50:11

我认为他的意思是你需要清单中的 RECORD_AUDIO 权限:

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

这对我有用。

I think he means you need the RECORD_AUDIO permission in the manifest:

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

That worked for me.

念三年u 2024-10-09 13:50:11

--编辑--

请参阅比尔的回答。

--end edit--

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

--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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文