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

本文是小编为大家收集整理的关于通过文字转语音说话时暂停手机铃声,然后再恢复的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在制作一个呼叫者说话的应用程序,它使用TTS说出来电者名称.我想在TTS说话时暂停铃声,然后恢复铃声.从我研究的研究我们可以使用AudioFocus(希望所以). 无论如何,我使用以下代码

更新

我现在正在使用这个代码.

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之后播放播放Ringtone之后.我该怎么办?

推荐答案

终于在刮擦我的头后两天我终于做到了.对于所有想要实现这样的东西但无法这样做的人来说,这是代码

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 - 获取当前的铃声 .i.e铃声卷中的当前音量.

musicVolume - 获取 music

的当前音量

currentRingVolume只需保留ringVolume.

注 - STREAM_RING和STREAM_MUSIC是不同的.请参阅不同的卷

现在,基本思想是mute ringtone,而TTS正在发言,然后将其设置为先前的值.

seekBarValue - 是我的SeekBar,描绘了TTS卷w.r.t musicVolume的水平,是可选的.

PauseRingtone - 是一个CheckBox Preference,它检查我们是否想要在说话时暂停铃声.如果true以AudioManager.STREAM_RING设置为1 i.e. vibrate else ringVolume i.e. Phone Value,同时播放TTS和Ringtone.

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

将TTS的音量设置为musicVolume.在TTS完成时,在onDone()中,我们使用currentRingVolume将Ringtone返回到ringVolume的音量.

如果我的答案帮助标记了我的答案有用.

其他推荐答案

摆脱此焦点侦听器并添加此代码侦听侦听器类.这将解决您的问题.

 int mode = audioManager.getRingerMode();
 int musicVolume= audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0);
 audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

if(phoneState == TelephonyManager.CALL_STATE_IDLE)
{

 audioManager.setRingerMode(mode);

}

本文地址:https://www.itbaoku.cn/post/102564.html

问题描述

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

Update

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

}

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

推荐答案

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

}

explaination :-

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

musicVolume - gets the current volume of the music

currentRingVolume just retains the ringVolume.

Note- STREAM_RING and STREAM_MUSIC are different things. See different volumes

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

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

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) 

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.

If my answer helped mark my answer useful.

其他推荐答案

Get rid of this focus listener and add this code phoneStateListener class. This will solve your problem.

 int mode = audioManager.getRingerMode();
 int musicVolume= audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0);
 audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

if(phoneState == TelephonyManager.CALL_STATE_IDLE)
{

 audioManager.setRingerMode(mode);

}