【问题标题】:My "return" statement isn't working even when there are no other code branches即使没有其他代码分支,我的“return”语句也不起作用
【发布时间】:2012-02-05 17:58:09
【问题描述】:

我有一个尝试创建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;
}

问题是return recorder 永远不会发生!

这是我的 logcat 输出:

在突出显示的行 (8000 / 3 / 12) 上没有错误,但也没有成功。

如果我没有使用下面的 cmets 中所说的 continue,它仍然不会返回!

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;
}

【问题讨论】:

  • 同样的模式(记录的尝试没有随后的成功或失败)在日志中也发生了 3 次。感觉就像 try/catch 块正在吃异常(或发出静默的 continue 语句)。您提供的代码示例是否正是生成了日志?
  • 这确实很奇怪。您是否尝试过测试逆条件并嵌套 if 块,因此不使用 continue
  • 就像@SeanReilly 提到的,看起来这不是生成日志输出的代码。除非您的代码清单中缺少break;continue;,否则现在似乎可以连续发送两条Trying 消息。您是否尝试过清理和重建您的应用程序以确保类是最新的?您是否尝试过使用调试器并单步执行代码?
  • 在此之前运行的唯一其他代码是:new RealTimeAudioRecorder().start();final AudioRecord audioRecord = getAudioRecord();。没有try-catch。 @Sean Reilly 是的,没错。
  • @JB Nizet - 我只是这样做了(问题中的代码),但它仍然不起作用!

标签: java android android-mediarecorder


【解决方案1】:

return 语句没有问题,您根本无法到达它,因为您的 AudioRecord 从未初始化(大多数示例都没有检查它,即使它们可能应该检查它)

作为一个简短的健全性检查,您可能希望首先将您的清单文件检查到 verify you have the proper permissions 以录制音频,因为对麦克风的访问是可能存在/可能不存在的硬件功能。

【讨论】:

    【解决方案2】:

    将您的函数包装在try/catch 块中,您可能会发现某些东西导致抛出异常。

    【讨论】:

    • 如果一个异常被抛出,它会进入调用栈直到被捕获。迭代将立即停止。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2013-12-22
    • 2021-07-08
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多