[英]Check and see if a sound is playing, wait for it to finish and then play another
我要完成的工作是单击按钮,然后播放基于子字符串的声音。
这是我当前按下按钮的代码:
display.setText("start");
thefull = thef.getText().toString();
for(int a=0;a<thefull.length();a++)
{
letter=thefull.substring(a,a+1);
if(letter=="m")
{
oursong = MediaPlayer.create(MainActivity.this, R.raw.m);
oursong.start();
while(oursong.isPlaying())
{
display.setText("start");
}
oursong.release();
}
else if(letter=="a")
{
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.start();
while(oursong.isPlaying())
{
display.setText("start");
}
oursong.release();
}
display.setText("done");
但是由于某些原因,当我单击我的按钮时,没有声音播放。 我也是android和java编程的新手,所以我这样做是否正确?
因为我真正想要的是让程序在单击按钮时是否检查声音是否正在播放(在本例中为“ oursound”),并且如果声音正在播放,我希望程序等待它完成以开始另一个声音之后,但是现在我的代码没有声音
首先,您需要先停止媒体播放器,然后再释放它(更安全)。
其次,不要使用while(oursong.isPlaying()) { display.setText("start"); }
while(oursong.isPlaying()) { display.setText("start"); }
; 您必须使用setOnCompletionListener方法注册OnCompletionListener。 播放歌曲时,将调用endCompletion。 像这样
MediaPlayer mp = new MediaPlayer();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
player.release();
}});
并且在onComplete内部应重置文本“完成”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.