Stopping sounds looping in J2ME
I am c开发者_高级运维urrently adding sound to my Rock, Paper, Scissors J2ME app. At the moment I can get the sound to play, but it plays in a continuous loop which will not stop. I have called stop();
and this does not seem to help. Below is the code I have used to create the sound:
private void playSound(String sound)
{
try{
InputStream in = getClass().getResourceAsStream(sound+".wav");
Player p = Manager.createPlayer(in, "audio/x-wav");
p.start();
p.stop();
//System.out.println("sound playing...");
}
catch(Exception e)
{
System.out.println("Sound error:" + e);
}
}
How can I make the sound stop?
It looks like you are using MMAPI and MIDP API. If that's the case then the way you use to test how stop
works is unlikely to work as you expect. Look:
p.start();
p.stop();
If you invoke stop immediately after start, there is a chance that Player will ignore it because it is not yet in state ready to be stopped.
If you want to learn how stop works, add a command to stop to the screen and invoke it after you hear the sound playing. At that moment it is reasonable to expect player to be in the state ready to stop.
精彩评论