Reputation: 15738
my code is this
bv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.pause();
bv.setImageResource(R.drawable.playzz);
} else {
bv.setImageResource(R.drawable.pausezz);
for (int i=1; i<=10 ; i++){
mp.start();
}
}
}
I wanted to repeat a song for only 10 time. i used for loop but the sound plays only one time and stops. Any idea how to do this?? thanks in advance.
Upvotes: 0
Views: 5069
Reputation: 69388
You should do this in a separate thread (assuming mp is a field):
mp.setOnCompletionListener(new OnCompletionListener() {
int n = 0;
@Override
public void onCompletion(MediaPlayer mp) {
if (n < 10) {
mp.start();
n++;
}
}
});
mp.start();
doing
while(mp.isPlaying());
will eat your CPU.
Upvotes: 6
Reputation: 853
I believe that the song starts 10 times without waiting for the first instance to end, so what you need to do is check if the song is still playing and start it again only after it has stopped playing. Maybe something like this -
for (int i=1; i<=10 ; i++){
mp.start();
while(mp.isPlaying());
}
This will make sure that the next instance of the song starts only after the current instance has finished. Also you need to run this in the background and not in onClick so that it won't interrupt any other foreground process.
Upvotes: 0