gpt4 book ai didi

android - AudioRecorder 在尝试所有变体后无法初始化

转载 作者:行者123 更新时间:2023-11-29 00:26:14 25 4
gpt4 key购买 nike

我看了很多关于 AudioRecorder 初始化问题的帖子,所以我用一个函数来测试每一种可能性。不幸的是,没有工作的。我应该检查什么?

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w("DEBUG", "Audio record");

// Capture mono data at 16kHz
int frequency = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
AudioRecord audioRecord=null;

// The minimal buffer size CANNOT be merely 20ms of data, it must be
// at least 1024 bytes in this case, this is most likely due to a MMIO
// hardware limit.
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
Log.w("DEBUG", "Buffer size: "+bufferSize);
try
{

// audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
// frequency, channelConfiguration,
// audioEncoding, bufferSize);
audioRecord=findAudioRecord();

short[] buffer = new short[bufferSize];
if(audioRecord==null || audioRecord.getState()!= AudioRecord.STATE_INITIALIZED)
{
Log.w("DEBUG", "Can't start");
//Log.w("DEBUG", "status: " + audioRecord.getState());
return;
}

audioRecord.startRecording();


// Blocking loop uses about 40% of the CPU to do this.
int sampleNumber = 0;

// We'll capture 3000 samples of 20ms each,
// giving us 60 seconds of audio data.
while (sampleNumber < 3000)
{
audioRecord.read(buffer, 0, 320);

// for (int i = 0; i < buffer.length; i++)
// {
// fileBuffer[i * 2] = (byte) (buffer[i] & (short) 0xFF);
// fileBuffer[i * 2 + 1] = (byte) (buffer[i] >> 8);
// }


sampleNumber++;
}
} catch (IllegalArgumentException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalStateException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
finally
{
if(audioRecord!=null && audioRecord.getState()==AudioRecord.STATE_INITIALIZED)
{
audioRecord.stop();
audioRecord.release();
}
}


}

private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
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.w("DEBUG", "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(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

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

这是输出:

10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 16 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 12 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 16 10-06 22:18:33.867: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 2, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 16 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 3, channel: 12 10-06 22:18:33.875: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 16 10-06 22:18:33.906: WARN/DEBUG(26097): Attempting rate 11025Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 22050Hz, bits: 2, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 16 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 3, channel: 12 10-06 22:18:33.929: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 16 10-06 22:18:33.937: WARN/DEBUG(26097): Attempting rate 44100Hz, bits: 2, channel: 12 10-06 22:18:33.937: WARN/DEBUG(26097): Can't start

list 中有以下内容:

uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" uses-permission android:name="android.permission.RECORD_AUDIO"

我的手机是 Galaxy Ace。我应该检查什么?

最佳答案

我重新启动了手机,现在可以使用了。我想,在每种情况下调用 release()(请参阅 finally block )是必不可少的

关于android - AudioRecorder 在尝试所有变体后无法初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213531/

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