检查并查看是否有播放声音时,等待它完成,然后再踢 [英] Check and see if a sound is playing, wait for it to finish and then play another

查看:742
本文介绍了检查并查看是否有播放声音时,等待它完成,然后再踢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么,我想acomplish对我来说是一个按钮基于一个子单击,然后播放声音。

What i am trying to acomplish is for me to click on a button and then play a sound based on a substring.

这是我目前的code为pressing按钮:

This is my current code for pressing the button :

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编程,所以我要去权这样做?

but for some reason when i click my button no sound is played. I am also new to android and java programming, so am i going right about doing this?

由于我真正想要的是让程序继续检查,如果声音播放(在这种情况下,oursound)单击该按钮,如果播放声音时,我想程序等待它完成时启动之后它另一种声音。
但现在我的code doesn't发出任何声音

Because what i really want is for the program to keep checking if a sound is playing(in this case "oursound) when the button is clicked and if the sound is playing i want the program to wait for it to finish to start another sound right after it. But for now my code doesn´t play any sounds

推荐答案

所有你需要释放它之前停止媒体播放器的第一个(它的安全。)

First of all you need to stop the media player before releasing it(its safer.)

二,而不是使用而(oursong.isPlaying())
{
display.setText(开始);
}
;你必须注册OnCompletionListener使用setOnCompletionListener方法。当歌曲播放endCompletion将被调用。像这样;

Second, instead of using while(oursong.isPlaying()) { display.setText("start"); } ; you have to register OnCompletionListener using setOnCompletionListener method. When the song is played endCompletion will be called. Like so;

MediaPlayer mp = new MediaPlayer();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
   player.release();          
}});

的onComplete 应该重置文本完成

这篇关于检查并查看是否有播放声音时,等待它完成,然后再踢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆