尝试录制音频但崩溃

时间:2019-07-26 07:44:44

标签: java android android-mediarecorder android-audiorecord

我试图弄清楚为什么我的应用程序崩溃了。我只是想录制音频并将其读入缓冲区。但是,每次我按记录时,它都会崩溃。我不确定为什么。

我知道它在代码的recording.startRecording();部分崩溃。有人可以帮忙吗。

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button recordButton;

    private AudioRecord recording;
    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    double[] dbSample = new double[bufferSize];
    short[] sample = new short[bufferSize];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recordButton = findViewById(R.id.recordButton);
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recording = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, RECORDER_SAMPLERATE,
                                            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
                recording.startRecording();
                int bufferReadResult = recording.read(sample, 0, bufferSize);
                for (int j = 0; j < bufferSize && j < bufferReadResult; j++) {
                    dbSample[j] = (double) sample[j];
                }
            }
        });

    }
}

2019-07-26 17:40:48.027 7915-7915/com.example.test E/IAudioFlinger: createRecord returned error -22
2019-07-26 17:40:48.027 7915-7915/com.example.test E/AudioRecord: AudioFlinger could not create record track, status: -22
2019-07-26 17:40:48.027 7915-7915/com.example.test E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.
2019-07-26 17:40:48.030 7915-7915/com.example.test E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-07-26 17:40:48.031 7915-7915/com.example.test D/AndroidRuntime: Shutting down VM
2019-07-26 17:40:48.033 7915-7915/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test, PID: 7915
    java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
        at android.media.AudioRecord.startRecording(AudioRecord.java:992)
        at com.example.test.MainActivity$1.onClick(MainActivity.java:38)
        at android.view.View.performClick(View.java:7339)
        at android.widget.TextView.performClick(TextView.java:14222)
        at android.view.View.performClickInternal(View.java:7305)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27788)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7073)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2019-07-26 17:40:48.059 7915-7915/com.example.test I/Process: Sending signal. PID: 7915 SIG: 9

2 个答案:

答案 0 :(得分:0)

该错误来自java.lang.IllegalStateException:在未初始化的AudioRecord上调用了startRecording()。

您是否已向AndroidManifest添加权限?

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

如果仍然无法正常工作

使用AudioRecord的窍门是每个设备可能具有不同的初始化设置,因此您将必须创建一种循环遍历所有可能的比特率,编码等的方法。

public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    Log.d(C.TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e(C.TAG, rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}

AudioRecord recorder = findAudioRecord();
recorder.release();

参考https://stackoverflow.com/a/5440517/11732119

答案 1 :(得分:0)

您是否要求录制音频许可?