14

我的问题很容易解释——我试图创建一个 AudioRecord 对象,但它无法初始化(即在构造函数之后,getState 返回 0,表示失败)。我在运行 OS 2.2.1 的 MotoDroid 1 上从 Eclipse 运行它。我的 AndroidManifest.xml 是,AFAIK,使用正确的权限,RECORD_AUDIO(我不知道如何确认):

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <activity android:name=".SphinxMic"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我执行以下操作来创建 AudioRecord:

bufferSize = AudioRecord.getMinBufferSize(8000, CHANNEL_IN_MONO, ENCODING_PCM_8BIT);
audioRecorder = new AudioRecord(AudioSource.MIC, 8000, CHANNEL_IN_MONO, ENCODING_PCM_8BIT, 50*bufferSize);
if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
  throw new Exception("AudioRecord init failed");

audioRecorder.getState() 返回 0(即 STATE_UNINITIALIZED)

我还没有找到任何使用此 API 的完整示例,而且我是一名 Android 初学者,所以解决方案可能很简单。我该怎么做才能找出失败的原因?

一些人提出了类似的问题,但他们肯定遇到了与我不同的问题,因为他们批准的修复对我没有帮助。最值得注意的是这个。但是批准的解决方案令人困惑,无论如何都对我不起作用。我还尝试了各种比特率(8000、16000、11025、44100),包括单声道和立体声以及 8 位和 16 位。没有组合返回成功初始化。

4

4 回答 4

42

我花了几个小时解决这个问题,发现移动

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

在应用程序块之外实际上解决了它!

...
    </application>

    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
</manifest>
于 2011-08-25T09:46:05.320 回答
7

另一个可能的问题:

RECORD_AUDIO在清单中启用了权限,但对于 API 级别 23,我还必须在运行时请求权限。

于 2016-07-02T20:01:28.677 回答
1

试试 16 位。这对我有用:

try {
        // Create a new AudioRecord object to record the audio.
        bufferSize = AudioRecord.getMinBufferSize(frequency,channelConfiguration,audioEncoding);

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                                                  frequency, channelConfiguration, 
                                                  audioEncoding, bufferSize);
    } catch (Throwable t) {
        Log.e("AudioRecord","Recording Failed");
    }

我设置了以下变量:

int frequency = 8000;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
于 2010-12-19T20:02:47.023 回答
0

如果有人需要为 API 23+ 手动请求磁盘权限,这是我使用的一种方法。只需交换permission.RECORD_AUDIO相关位即可。方法逻辑依赖于一些外部位(例如strings.xml, ActivityCompat, Snackbar),但认为它可能对某人有所帮助。

/**
 * Requests the Storage permissions.
 * If the permission has been denied previously,
 * a SnackBar will prompt the user to grant the
 * permission, otherwise it is requested directly.
 */
private void requestStoragePermissions() {

    // Newer versions Android 18+
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
        /**
         * Permissions required to read and write storage.
         */
        final String[] PERMISSIONS_STORAGE = {
                permission.READ_EXTERNAL_STORAGE,
                permission.WRITE_EXTERNAL_STORAGE,
        };

        final boolean readResult =
                ActivityCompat.shouldShowRequestPermissionRationale(
                        this, permission.READ_EXTERNAL_STORAGE);
        final boolean writeResult =
                ActivityCompat.shouldShowRequestPermissionRationale(
                        this, permission.WRITE_EXTERNAL_STORAGE);

        if (readResult || writeResult) {

            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example, if the request has been denied previously.
            Log.i(TAG,
                    "Displaying disk permission rationale to provide additional context.");

            // Display a SnackBar with an explanation and a button to trigger the request.
            Snackbar.make(mainView, R.string.permission_storage_rationale,
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(R.string.ok, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat
                                    .requestPermissions(MainActivity.this,
                                            PERMISSIONS_STORAGE,
                                            REQUEST_STORAGE);
                        }
                    })
                    .show();
        } else {
            // Storage permissions have not been granted. Request them directly.
            ActivityCompat.requestPermissions(
                    this, PERMISSIONS_STORAGE, REQUEST_STORAGE);
        }

    }
}
于 2017-07-27T20:02:18.607 回答