简体   繁体   English

如何点按以停止Android中的公告?

[英]How to tap to stop announcements in android?

I use the following code to speak out voice, it works well. 我使用以下代码说出声音,效果很好。

Now I hope to add a function to tap to stop announcements, by do this , the customer can cancel speech, I don't know how to it, could you help me? 现在,我希望添加一个功能来点按以停止通知,这样,客户可以取消语音,我不知道该如何处理,您能帮我吗? thanks! 谢谢!

public class SpeechTxt {
    private static SoftReference<TextToSpeech> sTts;
    private static boolean isNeedBackup=true;

    public static void SpeakOut(final Context context, final String s) {
        final Context appContext = context.getApplicationContext();
        if (sTts == null) {
           sTts = new SoftReference<TextToSpeech>(new TextToSpeech(appContext,
                    new TextToSpeech.OnInitListener() {
                        @Override
                        public void onInit(int status) {
                            if (status == TextToSpeech.SUCCESS) {
                                speak(appContext, s);
                            } else {
                                loadText2SpeechData(appContext);
                            }
                        }
                    }));
        } else {
           speak(appContext, s);
        }
    }


    private static void speak(Context context, String s) {
        if (sTts != null) {
            switch (sTts.get().setLanguage(Locale.getDefault())) {
              case TextToSpeech.LANG_COUNTRY_AVAILABLE:
              case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
              case TextToSpeech.LANG_AVAILABLE: {
                  speakWhenOK(context, s);
                  break;
              }
              case TextToSpeech.LANG_MISSING_DATA: {
                  loadText2SpeechData(context);
                  break;
              }
              case TextToSpeech.LANG_NOT_SUPPORTED:
            }
        }
    }

    @SuppressLint("NewApi")
    private static void speakWhenOK(Context context, String s){ 
        final Context myContext = context;  

        SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(context);


        HashMap<String, String> ttsParameters = new HashMap<String, String>();

        ttsParameters.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,String.valueOf(Math.random()) );   

        sTts.get().speak(s, TextToSpeech.QUEUE_ADD, ttsParameters);
    }   
}

give an id to your rootview, and then (eg. ROOT) then initialize it, and then 给您的rootview一个id,然后(例如ROOT)初始化它,然后

try this : 尝试这个 :

RootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
            //
            //  run your stop(), for speech here
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM