开发者

Android MediaPlayer SDK

I have a prog开发者_StackOverflow社区ram with a function copied below it plays a sound upon clicking a button. If you click the button 10 times 10 different media players play the same sound that is the way i want it but how can i assign a button to stop all 10 media players at a time like a "STOP ALL BUTTON"

public void onClick(View v){
 int resId = 0;
 int stopped = 0;
 switch (v.getId()){
 case R.id.Wail:
  resId = R.raw.fedsigwail;
  break;
 case R.id.Yelp:
  resId = R.raw.fedsigyelp;
  break;
 case R.id.HiLow:
  resId = R.raw.hilow;     
  break;
 case R.id.FederalQ:
  resId = R.raw.federalq;
  break;
 case R.id.Horn:
  resId = R.raw.fedsignhorn;
  break;
 case R.id.STOPALL:
  mp.stop();
  mp.release();
  stopped = 1;
  break;
 }

 if (stopped != 1){
 mp = MediaPlayer.create(this, resId);
 mp.start();
 }


}

The code above only stops the last instance of mp.

Any Input would be appreciated


mp.stop(); only stops one instance of a media player, since mp can only be one mediaplayer instance. If mp is one of your media players, where are the other ones? You mentioned you have

10 different media players

so I would expect something like mp1, mp2, mp3, ..., mp10. But right now it seems you actually only using one media player instance, or at least stopping it.

With each call of

mp = MediaPlayer.create(this, resId)

you lose the reference to the previous created media player, since you 'override' it with a newly created instance. You need to keep a reference to all your created media players, i.e. via an ArrayList or HashMap or similar.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜