ToneGenerator 声音流畅吗?

发布于 2024-12-02 03:15:34 字数 1365 浏览 0 评论 0原文

有人有同样的问题吗?

在 Android 的电话应用程序上,每当按下数字时,铃声总是平稳的。

但是,当我使用与“电话”应用程序非常相似的代码时,我经常听到的语气并不流畅……存在间隙。有办法解决这个问题吗?

我的一些理论是,由于处理过程中存在时间滞后,模拟器会导致声音中断。模拟器上的电话应用程序也比我的代码更加“编译”/本机。等等。不知道是什么原因导致这些音调不连续。

这是代码(实际上与电话应用程序相同):

...
playTone(ToneGenerator.TONE_DTMF_1,150);
...


void playTone(int tone) {
    // if local tone playback is disabled, just return.
    if (!mDTMFToneEnabled) {
        return;
    }

    // Also do nothing if the phone is in silent mode.
    // We need to re-check the ringer mode for *every* playTone()
    // call, rather than keeping a local flag that's updated in
    // onResume(), since it's possible to toggle silent mode without
    // leaving the current activity (via the ENDCALL-longpress menu.)
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int ringerMode = audioManager.getRingerMode();
    if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
            || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
        return;
    }

    synchronized (mToneGeneratorLock) {
        if (mToneGenerator == null) {
            Log.w("test", "playTone: mToneGenerator == null, tone: " + tone);
            return;
        }

        // Start the new tone (will stop any playing tone)
        mToneGenerator.startTone(tone, TONE_LENGTH_MS);

Does anyone have the same problem?

On Android's Phone app, whenever a number is pressed, the tone is always smooth.

But, when I used very similar code as the Phone app, the tone I get regularly isn't smooth...there are gaps. Is there a way to resolve this?

Some of my theories are that the emulator causes these breaks in sound as there are time lags in processing. The Phone app on the emulator is also more "compiled"/native than my code. Etc. Don't know what is the reason for these tones not being continuous.

Here's the code (literally the same as Phone app):

...
playTone(ToneGenerator.TONE_DTMF_1,150);
...


void playTone(int tone) {
    // if local tone playback is disabled, just return.
    if (!mDTMFToneEnabled) {
        return;
    }

    // Also do nothing if the phone is in silent mode.
    // We need to re-check the ringer mode for *every* playTone()
    // call, rather than keeping a local flag that's updated in
    // onResume(), since it's possible to toggle silent mode without
    // leaving the current activity (via the ENDCALL-longpress menu.)
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int ringerMode = audioManager.getRingerMode();
    if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
            || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
        return;
    }

    synchronized (mToneGeneratorLock) {
        if (mToneGenerator == null) {
            Log.w("test", "playTone: mToneGenerator == null, tone: " + tone);
            return;
        }

        // Start the new tone (will stop any playing tone)
        mToneGenerator.startTone(tone, TONE_LENGTH_MS);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文