文本到语音不能处理棉花糖中的来电

时间:2017-03-03 12:54:50

标签: android google-text-to-speech

此代码完美适用于Lollipop设备,但它不适用于任何Marshmallow设备。我尝试了很多东西,比如:

1)将STREAM_MUSIC更改为STREAM_RING

2)将STREAM_MUSIC更改为STREAM_SYSTEM

3)来电时阻止铃声播放

但这些都没有奏效。

          @SuppressWarnings("deprecation")
                private void ttsUnder20(String text) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");

                        speech.speak(text, TextToSpeech.QUEUE_FLUSH, map); //speak for API level<21
                    }


                enter code here
                @TargetApi(Build.VERSION_CODES.M)
                private void ttsGreater21(String text) {
                    bund.clear();
                    String utteranceId = this.hashCode() + "";
                    audio.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
                    //
                    // audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);


                        bund.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM,AudioManager.STREAM_MUSIC);
                        speech.speak(text, TextToSpeech.QUEUE_FLUSH, bund, "");
                    }



//check for read phone state permission when activity start.
 private void checkPermission() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.READ_PHONE_STATE)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_PHONE_STATE},
                        MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

                // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }

任何帮助都会得到赞赏。

0 个答案:

没有答案