我有一些代碼可以啓動文本到語音,並在按下按鈕時開始播放聲音。除了一個細節外,一切工作都很好。當連續按兩個按鈕時,聲音開始重疊。每次按下按鈕時是否有辦法切斷媒體播放器?我已經使用了noise1.pause(),noise1.stop()和noise1.reset(),但沒有任何效果。我正在考慮使用mediaplayer完整的監聽器,但我不知道如何實現這一點。任何幫助,將不勝感激。非常感謝!!!如何讓音樂停止點擊下一個按鈕
public void onClick(View view) {
Resources res = getResources();
final TextView tv = (TextView) findViewById(R.id.color_text);
final MediaPlayer noise1 = MediaPlayer.create(this, R.raw.one);
final MediaPlayer noise2 = MediaPlayer.create(this, R.raw.two);
final MediaPlayer noise3 = MediaPlayer.create(this, R.raw.three);
switch (view.getId()) {
case R.id.one_button:
String oneString = res.getString(R.string.One);
tv.setText(oneString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null, null);
}
noise1.start();
break;
case R.id.two_button:
String twoString = res.getString(R.string.Two);
tv.setText(twoString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null, null);
}
noise2.start();
break;
case R.id.three_button:
String threeString = res.getString(R.string.Three);
tv.setText(threeString);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null, null);
}
three.start();
break;
}
「_but nothing works_」爲什麼? –