当前位置: 首页 > 工具软件 > TextToSpeech > 使用案例 >

android TextToSpeech 静音时仍然播放声音

房育
2023-12-01

通过简单的设置,可以实现即使系统静音,也可以让textToSpeech发声。

代码如下:
ts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                        @Override
                        public void onInit(int status) {
                            if (status == TextToSpeech.SUCCESS) {
                                ts.setPitch(2f);//方法用来控制音调
                                ts.setSpeechRate(1.4f);//用来控制语速
//主要是以下三行起到作用了
                                AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder().
                                        setUsage(AudioAttributes.USAGE_ALARM).
                                        setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).
                                        setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
                                ts.setAudioAttributes(audioAttributes.build());
//主要是以上代码,设置应用场景为闹钟。
                                //判断是否支持下面两种语言
                                int result1 = ts.setLanguage(Locale.US);
                                int result2 = ts.setLanguage(Locale.SIMPLIFIED_CHINESE);
                                boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                                boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);
                                boolean reported = false;
                                if (min == 0) {
                                    ts.speak("现在时刻," + hour + "点整", TextToSpeech.QUEUE_FLUSH, null);
                                    reported = true;
                                } else if (min == 30) {
                                    ts.speak("现在时刻," + hour + "点三十分", TextToSpeech.QUEUE_FLUSH, null);
                                    reported = true;
                                } else if (min % 10 == 0) {
                                    ts.speak("" + hour + "点" + min + "分", TextToSpeech.QUEUE_ADD, null);
                                    reported = true;
                                }
                                if(reported){
                                    PendingIntent p = PendingIntent.getActivity(myService.this, 0, new Intent(myService.this, PandoraEntryActivity.class), 0);
                                    NotificationManager mNotifyMgr =  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                    Notification notification = new NotificationCompat.Builder(myService.this, NOTIFICATION_CHANNEL_ID)
                                            .setWhen(System.currentTimeMillis())
                                            .setSmallIcon(R.drawable.icon)
                                            .setContentTitle("整点半点报正在运行")
                                            .setContentText(hour + ":" + min)
                                            .setContentIntent(p)
                                            //在build()方法之前还可以添加其他方法
                                            .build();
                                    mNotifyMgr.notify(1, notification);
                                }
                            } else {
                                Toast.makeText(myService.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

代码摘自我写的一个整点半点报时器。之前没有这个静音报时功能。后来感觉不方便。加上去了。

 类似资料: