暂停电话铃声,同时通过文字说话的语音,然后恢复 [英] Pause Phone Ringtone while Speaking through Text To Speech and then Resume

查看:251
本文介绍了暂停电话铃声,同时通过文字说话的语音,然后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个来电者说话的应用程序采用TTS它说话呼叫人姓名。我要暂停的铃声,而TTS说话然后又继续铃声。从我的研究,我们可以使用 AudioFocus (希望如此)。 反正我用下面的code

I am making a caller speaking application which speak caller name using TTS. I want to pause the ringtone while TTS is speaking then resuming the ringtone. From what i have researched we can use AudioFocus (hope so). Anyway i am using the following code

更新

我用这code现在。

I am using this code now.

public void speak(final String talk) throws InterruptedException {
     final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
     int musicVolume= audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0);
     audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

    int result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

        @Override
        public void onStart(String utteranceId) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onError(String utteranceId) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDone(String utteranceId) {
            // TODO Auto-generated method stub
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            System.out.println("done");
        }
    });
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
    tts.speak(talk, TextToSpeech.QUEUE_FLUSH, params);
    System.out.println("speaking after tts is over" + talk+" "+result);

}

虽然铃声停止和 TTS 播放,但在 TTS 播放铃声将无法继续。我应该怎么办?

Although the Ringtone is stopped and tts is played but after tts is played Ringtone is not resumed. What should i do?

推荐答案

抓我的头两天,我终于做到了最后经过。对于所有那些谁想要实现这样的事情,但不能因此在这里做的是code

Finally after scratching my head for two days i finally did it. For all those who want to implement something like this but are unable to do so here is the code

public void speak(final String talk) throws InterruptedException {
    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int ringVolume = audioManager
            .getStreamVolume(AudioManager.STREAM_RING);
    int musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    currentRingVolume = ringVolume;

    musicVolume = (int) ((musicVolume * seekbarValue) / 100);

    if (PauseRingtone == true) {
        audioManager.setStreamVolume(AudioManager.STREAM_RING, 1,
                AudioManager.FLAG_SHOW_UI);
    } else {
        audioManager.setStreamVolume(AudioManager.STREAM_RING,
                ringVolume, AudioManager.FLAG_SHOW_UI);
    }

    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0);

    int result = tts
            .setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(String utteranceId) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onError(String utteranceId) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onDone(String utteranceId) {
                    // TODO Auto-generated method stub
                    System.out.println("done");
                    audioManager.setStreamVolume(AudioManager.STREAM_RING,
                            currentRingVolume, 0);
                }
            });
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "stringId");
    tts.speak(talk, TextToSpeech.QUEUE_FLUSH, params);
    System.out.println("speaking after tts is over" + talk + " " + result);

}

的解释: -

ringVolume - 得到的铃声的当前音量 .IE铃声音量在手机设置

ringVolume - gets the current volume of the ringtone .i.e ringtone volume set in the phone.

musicVolume - 得到的音乐当前的音量

currentRingVolume 只保留了 ringVolume

注意 - STREAM_RING STREAM_MUSIC 是不同的东西。请参阅

Note- STREAM_RING and STREAM_MUSIC are different things. See

现在的基本想法是静音铃声,而 TTS 在讲话,然后将其设置为previous值。

Now the basic idea is to mute the ringtone while TTS is speaking and then set it to previous value.

seekBarValue - 是我的搜索栏描绘了 TTS 卷WRT musicVolume 键,是可选的。

seekBarValue- is my SeekBar which depicts the level of the TTS volume w.r.t musicVolume and is optional.

PauseRingtone - 一个复选框preference 它检查是否要暂停的铃声,同时说话。如果是集 AudioManager.STREAM_RING 1 振动其他 ringVolume 手机值,因此这两个 TTS 铃声发挥在同一时间。

PauseRingtone- is a CheckBox Preference which checks whether we want to pause ringtone while speaking. If true is sets the AudioManager.STREAM_RING to 1 i.e. vibrate else ringVolume i.e. Phone Value, so both TTS and Ringtone play at the same time.

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,musicVolume, 0) 

设定 TTS 的卷 musicVolume 。经过 TTS 已完成上讲即 onDone()我们设置铃声<容积/ code>回 ringVolume 使用 currentRingVolume

sets the volume of TTS to musicVolume. After TTS has completed speaking i.e. in onDone() we set the volume of Ringtone back to the ringVolume using currentRingVolume.

如果我的回答帮助了纪念我的回答是有用的。

这篇关于暂停电话铃声,同时通过文字说话的语音,然后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆