我的“回归”即使没有其他代码分支,该语句也不起作用

发布于 2025-01-02 20:50:51 字数 2887 浏览 4 评论 0原文

我有一个尝试创建 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 输出:

Logcat output

在突出显示的行 (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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2025-01-09 20:50:51

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.

橙味迷妹 2025-01-09 20:50:51

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

Wrap your function in a try/catch block and you will probably find that something is causing an exception to be thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文