gpt4 book ai didi

java - 在 Android 中录制声音时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:17 26 4
gpt4 key购买 nike

我制作了一个应用程序来记录声音并分析它的频率。这个过程每秒重复几次,因此使用线程。

这在大多数情况下都有效,但出于某种原因,在第一次分析后,我在 logcat 中重复收到这些消息。

很少(但有时)在我测试时,应用程序没有记录任何声音。所以我认为它与此错误有关。

01-23 13:52:03.414: E/AudioRecord(3647): Could not get audio input for record source 1
01-23 13:52:03.424: E/AudioRecord-JNI(3647): Error creating AudioRecord instance: initialization check failed.
01-23 13:52:03.424: E/AudioRecord-Java(3647): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.

代码在下面,有人知道我哪里出错了吗?我没有正确杀死 AudioRecord 对象吗?代码已被修改以便于阅读:

public class recorderThread extends AsyncTask<Sprite, Void, Integer> {

short[] audioData;
int bufferSize;

@Override
protected Integer doInBackground(Sprite... ball) {

boolean recorded = false;
int sampleRate = 8192;
AudioRecord recorder = instatiateRecorder(sampleRate);


while (!recorded) { //loop until recording is running

if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
{
if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
recorder.startRecording();
//check to see if the Recorder has stopped or is not recording, and make it record.
else {
//read the PCM audio data into the audioData array

//get frequency
//checks if correct frequency, assigns number
int correctNo = correctNumber(frequency, note);

checkIfMultipleNotes(correctNo, max_index, frequency, sampleRate, magnitude, note);

if (audioDataIsNotEmpty())
recorded = true;

return correctNo;
}
}
else
{
recorded = false;
recorder = instatiateRecorder(sampleRate);
}
}

if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING)
{
killRecorder(recorder);
}

return 1;
}


private void killRecorder(AudioRecord recorder) {
recorder.stop(); //stop the recorder before ending the thread
recorder.release(); //release the recorders resources
recorder=null; //set the recorder to be garbage collected
}




@Override
protected void onPostExecute(Integer result) {
ballComp.hitCorrectNote = result;
}

private AudioRecord instatiateRecorder(int sampleRate) {
bufferSize= AudioRecord.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT)*2;
//get the buffer size to use with this audio record

AudioRecord recorder = new AudioRecord (AudioSource.MIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize);
//instantiate the AudioRecorder

audioData = new short [bufferSize];
//short array that pcm data is put into.
return recorder;
}
}

最佳答案

正如您的日志所说“无法获取记录源 1 的音频输入”,这意味着。 Android 设备未找到任何用于录制声音的硬件。

因此,如果您正在从模拟器测试应用程序,那么请确保您在录制声音期间已成功连接鼠标,或者如果您正在调试或从设备运行它,请确保麦克风已打开以录制声音.

希望对你有所帮助。

或者

如果以上没有解决您的问题,那么使用下面的代码来录制声音,因为它非常适合我。

代码:

  record.setOnClickListener(new View.OnClickListener() 
{
boolean mStartRecording = true;
public void onClick(View v)
{

if (mStartRecording==true)
{
//startRecording();
haveStartRecord=true;
String recordWord = wordValue.getText().toString();
String file = Environment.getExternalStorageDirectory().getAbsolutePath();
file = file+"/"+recordWord+".3gp";

System.out.println("Recording Start");
//record.setText("Stop recording");
record.setBackgroundDrawable(getResources().getDrawable( R.drawable.rec_on));
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(file);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

//mRecorder.setAudioChannels(1);//mRecorder.setAudioSamplingRate(8000);

                    try 
{
mRecorder.prepare();
}
catch (IOException e)
{
Log.e(LOG_TAG, "prepare() failed");
}

mRecorder.start();

}

else
{
//stopRecording();
System.out.println("Recording Stop");
record.setBackgroundDrawable(getResources().getDrawable( R.drawable.rec_off));
mRecorder.stop();
mRecorder.release();
mRecorder = null;
haveFinishRecord=true;

}
mStartRecording = !mStartRecording;

}
});

希望这个回答对你有帮助。

享受吧。 :)

关于java - 在 Android 中录制声音时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com