开发者

How to play one-shot sfx along with music

I've got an android app where I'm playing music through me开发者_开发知识库diaplayer and playing sfx using soundpool. From what I can find, mediaplayer doesn't have any callbacks to let you know when you hit certain points in a song\file, just a callback when it ends.

What is the best way to accomplish this? Should I start a thread that plays the sfx every certain amount of miliseconds? Or a while loop? Thanks


I think you can get the time lenght of your music and then use a simple timer to fire your sfx at some points (e.g. 5 seconds):

new Handler().postDelayed(new Runnable() {         
 @Override         
 public void run() {             
 //here call sfx
 }     
}, 5000);

Also with a count down timer you can make regular intervals and check with an if staments whether you want to play an sfx or not:

new CountDownTimer(60000, 1000) {       

 public void onTick(long millisUntilFinished) {          
   //call to my UI thread every one second      
 }       
 public void onFinish() {          
   //final call to my UI thread after 60 seconds     
 }   
}.start(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜