TTS 输出始终转到 A2DP

发布于 2024-11-28 07:57:48 字数 691 浏览 2 评论 0原文

我的 Android 教程指出,我可以明确告诉 TTS 引擎要使用哪个流:

对于音乐播放:

params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));

对于电话通话:

params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL));

我的理解是,音频路由到蓝牙耳机的工作原理是 STREAM_MUSIC 转到 < code>A2DP(又名 Android 蓝牙设置中的“媒体音频”)和 STREAM_VOICE_CALL 转到 HSP(又名 Android 蓝牙设置中的“电话音频”)。

但无论我在我的小应用程序中使用 STREAM_MUSIC 还是 STREAM_VOICE_CALL音频总是出于某种原因转至 A2DP

我做错了什么?有没有办法将 TTS 输出路由到耳机的 HSP 配置文件?

My Android tutorial states that I can explicitly tell the TTS engine which stream to use:

For music playback:

params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));

And for phone calls:

params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL));

My understanding is that audio routing to a Bluetooth headset works such that STREAM_MUSIC goes to A2DP (aka "media audio" in Android Bluetooth settings) and STREAM_VOICE_CALL goes to HSP (aka "phone audio" in Android Bluetooth settings).

But regardless whether I use STREAM_MUSIC or STREAM_VOICE_CALL in my little application, the audio always goes for some reason to A2DP.

What am I am doing wrong? Is there a way to route TTS output to headset's HSP profile?

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

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

发布评论

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

评论(3

荆棘i 2024-12-05 07:57:48

我在大多数设备上都能正常工作。下面是使用蓝牙 SCO 而不是 A2DP 在语音呼叫流上启动 TTS 的部分。

if (mTtsReady) {
    myHash = new HashMap<String, String>();

    myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "A2DP_Vol");

    OLD_AUDIO_MODE = am2.getMode();
    if(SMSstream == 1){
        if (am2.isBluetoothScoAvailableOffCall()) {
            am2.startBluetoothSco();
        }
        if(!am2.isSpeakerphoneOn()){
            speakerPhoneWasOn = false;
            am2.setSpeakerphoneOn(true);
        }
        myHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                String.valueOf(AudioManager.STREAM_VOICE_CALL));
        am2.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

    }
    else{
        am2.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        myHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                String.valueOf(AudioManager.STREAM_MUSIC));
    }

    new CountDownTimer(SMS_DELAY, SMS_DELAY/2) {

        @Override
        public void onFinish() {
            try {
                mTts.speak(str, TextToSpeech.QUEUE_ADD,
                        myHash);
            } catch (Exception e) {
                Toast.makeText(application,
                        R.string.TTSNotReady,
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }

        @Override
        public void onTick(long arg0) {

        }

    }.start();
}

现在我只是在完成后让流恢复回来时遇到问题。阅读 TTS 一切正常。它将暂停所有音乐,播放 TTS,然后正常恢复音乐。但是,当我稍后退出应用程序时,媒体流现在会通过手机耳机播放,直到我重新启动。我在这里发布了这个问题:使用 AudioManager 后音频流保留在耳机上

你可以在这里看到我的整个项目:http://code.google.com/p/a2dpvolume/

I got this working for the most part, on most devices. Here is the part that starts the TTS on the voice call stream using Bluetooth SCO instead of A2DP.

if (mTtsReady) {
    myHash = new HashMap<String, String>();

    myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "A2DP_Vol");

    OLD_AUDIO_MODE = am2.getMode();
    if(SMSstream == 1){
        if (am2.isBluetoothScoAvailableOffCall()) {
            am2.startBluetoothSco();
        }
        if(!am2.isSpeakerphoneOn()){
            speakerPhoneWasOn = false;
            am2.setSpeakerphoneOn(true);
        }
        myHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                String.valueOf(AudioManager.STREAM_VOICE_CALL));
        am2.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

    }
    else{
        am2.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        myHash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                String.valueOf(AudioManager.STREAM_MUSIC));
    }

    new CountDownTimer(SMS_DELAY, SMS_DELAY/2) {

        @Override
        public void onFinish() {
            try {
                mTts.speak(str, TextToSpeech.QUEUE_ADD,
                        myHash);
            } catch (Exception e) {
                Toast.makeText(application,
                        R.string.TTSNotReady,
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }

        @Override
        public void onTick(long arg0) {

        }

    }.start();
}

Now I just have a problem getting the stream to revert back when done. It all works fine to read TTS. It will pause any music, play the TTS, and then resume music fine. However, when I exit the app later the stream for media now plays through the phone earpiece until I reboot. I posted that question here: Audio stream stays on earpiece after using AudioManager

You can see my whole project here: http://code.google.com/p/a2dpvolume/

黯然 2024-12-05 07:57:48

如果您的耳机与 a2dp 配置文件兼容,则使用 AudioManager.STREAM_MUSIC 并通过流播放音频应该可以完成工作。

我还要补充一点,如果您当前正在通话中,并且通过语音流播放音频,则任何耳机(a2dp 或其他耳机)都可以听到音频。不幸的是,您需要通话。
不幸的是,我发现将模式设置为 MODE_IN_CALL 没有任何作用。

总结一下:
如果您只想播放音乐(不在通话时),则使用 AudioManager.STREAM_MUSIC 并且如果耳机兼容 A2DP,则它将听到音乐。

另外,请查看 AudioManager.isBluetoothA2dpOn(),以确保系统认为您的耳机已插入。

If your headset is compatible with the a2dp profile, then using AudioManager.STREAM_MUSIC and hence playing audio through the stream should get the job done.

I would also add, that if you are currently in a call, and you play audio through the voice stream, then any headset (a2dp or otherwise) can hear the audio. Unfortunately, you need to be in a call.
Unfortunately I have found that setting the mode to MODE_IN_CALL does nothing.

To sum it up:
If all that you are trying to do is play music (when not in a call), then use the AudioManager.STREAM_MUSIC and if the headset is A2DP compatible, then it will hear the music.

Also, take a look at AudioManager.isBluetoothA2dpOn(), to make sure that the system thinks that your headeset is plugged in.

橘香 2024-12-05 07:57:48

是的,可以通过非 A2DP 耳机播放 TTS(文本转语音)输出,就像 jroal 所说的那样,使用 SCO。

使用 AudioManager startBluetoothSco 启用 SCO。然后侦听 AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED 广播,并将 EXTRA_SCO_AUDIO_STATE 设置为 SCO_AUDIO_STATE_CONNECTED。

如果您现在对 STREAM_VOICE_CALL 流执行 TextToSpeech,它将最终出现在耳机中(即使是便宜的非 A2DP 设备)。

Yes it is possible to play the TTS (text-to-speech) output through a non A2DP headset like jroal says by using SCO.

Use AudioManager startBluetoothSco to enable SCO. Then listen for AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED broadcast with the EXTRA_SCO_AUDIO_STATE set to SCO_AUDIO_STATE_CONNECTED.

If you now do TextToSpeech to the STREAM_VOICE_CALL stream, it will end up in the headset (even the cheap non A2DP devices).

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