android - 如何在api 21中使用TextToSpeech

时间:2015-12-10 18:48:12

标签: android text-to-speech

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    private TextToSpeech tts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(this,this);

        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {

                if(state== TelephonyManager.CALL_STATE_RINGING ){
                    tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null);
                    Toast.makeText(getApplicationContext(),"Phone is Ringing : "+incomingNumber,Toast.LENGTH_LONG).show();
                }
            }
        };
    }

    @Override
    public void onInit(int status) {

    }
}

但是在API级别21中不推荐使用函数speak(String text, int queueMode, HashMap params)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

是的,您已经过时,不推荐使用,直到您过去使用 API 21

public int speak (String text, int queueMode, HashMap<String, String> params)

但此方法已在 API 21 中弃用。 从API级别21开始,替换为:

public int speak (CharSequence text, int queueMode, Bundle params, String utteranceId)

在你的情况下:

...    
tts.speak(incomingNumber+" calling", TextToSpeech.QUEUE_FLUSH, null, null);

只需将null传递给utteranceId参数

即可

有关Text To Speech

的更多信息