我的“回归”即使没有其他代码分支,该语句也不起作用
我有一个尝试创建 AudioRecord
的方法。不同的手机支持不同的采样率、通道配置和音频格式。因此该方法尝试为每个音频创建一个 AudioRecord 并返回第一个有效的。
private AudioRecord getAudioRecord() {
for (int rate: sampleRates) {
for (int audioFormat: audioFormats) {
for (int channelConfig: channelConfigs) {
String description = rate + "Hz, bits: " + audioFormat
+ ", channel: " + channelConfig;
Log.d(TAG, "Trying: " + description);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize == AudioRecord.ERROR
|| bufferSize == AudioRecord.ERROR_BAD_VALUE) {
Log.d(TAG, "Failed: This rate/channel config/format is not supported");
continue;
}
AudioRecord recorder = new AudioRecord(AudioSource.MIC, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_UNINITIALIZED) {
Log.d(TAG, "Failed: Recorder is uninitialized");
continue;
}
Log.d(TAG, "Success: " + description);
return recorder;
}
}
}
Log.e(TAG, "Failed all rates. Does the device have a microphone?");
return null;
}
问题是返回记录器
永远不会发生!
这是我的 logcat 输出:
在突出显示的行 (8000 / 3 / 12) 上没有错误,但也没有成功。
如果我按照下面的评论中所述不使用继续
,它仍然不会返回!
private AudioRecord getAudioRecord() {
for (int rate: sampleRates) {
for (int audioFormat: audioFormats) {
for (int channelConfig: channelConfigs) {
String description = rate + "Hz, bits: " + audioFormat
+ ", channel: " + channelConfig;
Log.d(TAG, "Trying (2): " + description);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR && bufferSize != AudioRecord.ERROR_BAD_VALUE) {
AudioRecord recorder = new AudioRecord(AudioSource.MIC, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
Log.d(TAG, "Success: " + description);
return recorder;
} else {
Log.d(TAG, "Failed: Recorder is uninitialized");
}
} else {
Log.d(TAG, "Failed: This rate/channel config/format is not supported");
}
}
}
}
Log.e(TAG, "Failed all rates. Does the device have a microphone?");
return null;
}
I have a method that attempts to create an AudioRecord
. Different phones support different sample rates, channel configs and audio formats. So the method tries to create an AudioRecord
for each of them and return the first that works.
private AudioRecord getAudioRecord() {
for (int rate: sampleRates) {
for (int audioFormat: audioFormats) {
for (int channelConfig: channelConfigs) {
String description = rate + "Hz, bits: " + audioFormat
+ ", channel: " + channelConfig;
Log.d(TAG, "Trying: " + description);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize == AudioRecord.ERROR
|| bufferSize == AudioRecord.ERROR_BAD_VALUE) {
Log.d(TAG, "Failed: This rate/channel config/format is not supported");
continue;
}
AudioRecord recorder = new AudioRecord(AudioSource.MIC, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_UNINITIALIZED) {
Log.d(TAG, "Failed: Recorder is uninitialized");
continue;
}
Log.d(TAG, "Success: " + description);
return recorder;
}
}
}
Log.e(TAG, "Failed all rates. Does the device have a microphone?");
return null;
}
The problem is return recorder
never happens!
Here is my logcat output:
On the highlighted line (8000 / 3 / 12) there is no error, but also no success.
If I use no continue
as said in the comments below, it still doesn't return!
private AudioRecord getAudioRecord() {
for (int rate: sampleRates) {
for (int audioFormat: audioFormats) {
for (int channelConfig: channelConfigs) {
String description = rate + "Hz, bits: " + audioFormat
+ ", channel: " + channelConfig;
Log.d(TAG, "Trying (2): " + description);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR && bufferSize != AudioRecord.ERROR_BAD_VALUE) {
AudioRecord recorder = new AudioRecord(AudioSource.MIC, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
Log.d(TAG, "Success: " + description);
return recorder;
} else {
Log.d(TAG, "Failed: Recorder is uninitialized");
}
} else {
Log.d(TAG, "Failed: This rate/channel config/format is not supported");
}
}
}
}
Log.e(TAG, "Failed all rates. Does the device have a microphone?");
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
return 语句没有问题,您只是永远无法到达它,因为您的 AudioRecord 从未初始化(大多数示例都不会检查它,尽管它们可能应该检查)
作为一个简短的健全性检查,您可能需要检查您的 Manifest 文件以 < a href="https://stackoverflow.com/questions/4161666/android-audiorecord-fails-to-initialize">首先验证您是否拥有录制音频的适当权限,前提是对麦克风的访问是一项可能存在/可能不存在的硬件功能。
There's no problem with the return statement, you simply never reach it because your AudioRecord never initializes (Most examples out there don't check it, even though they probably should)
As a brief sanity check you may want to check your Manifest file to verify you have the proper permissions to record audio in the first place, given that access to the microphone is a hardware feature that may/may not exist.
将您的函数包装在
try
/catch
块中,您可能会发现某些东西导致抛出异常。Wrap your function in a
try
/catch
block and you will probably find that something is causing an exception to be thrown.