【发布时间】:2016-08-03 01:55:17
【问题描述】:
我有一些代码可以启动文本到语音并在按下按钮时开始播放声音。除了一个细节外,一切都很好。当连续按下两个按钮时,声音开始重叠。有没有办法在每次按下按钮时切断媒体播放器?我使用了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;
}
【问题讨论】:
-
"但没有任何效果" 为什么?
标签: java android android-mediaplayer