I simply have a sound that executes on a button click. Everything works fine in the emulator but sound #2 plays in an infinite loop when I run on my phone.
Could this be a phone issue I have downloaded other apps from the market that have had these weird sound loops. (I have a Droid 2)
Declarations:
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.sound1);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound2);
Button 1 code is as follows.
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp1.start();
textView1.setText("Hello");
}
});
Button 2 code is as follows.(This is the loopy one)
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++;
mp2.start();
iv1.setVisibility(View.VISIBLE);
if (count == 5) {
//I do some enabling of components in here
}
} 开发者_运维知识库
});
I have tried
.stop()
.setLooping()
.pause()
as well as moving .start()
after and before the if statement
All of these result in it still looping or no sound at all.
If you swap R.raw.sound1 with R.raw.sound2 does the looping problem stay with the 2nd media player or does it follow sound2?
I'm not an expert on the format of sound files, but it could be a problem with the file itself. Try opening the file in Audacity and cutting off the last 10 ms then save it as something new and see if the problem exists in the new file.
Try to add OnCompletionListener for this MediaPlayers with
mp.stop();
http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
Hope, it help you
精彩评论