Reputation: 41
I have problem restart sound in J2ME. I want to create player in constructor, then call playExplosion() to play it, but it only plays once. I'm using Wireless Toolkit.
Sound class
public class Sound () {
private Player playerExplosion;
public Sound() throws IOException, MediaException {
// Explosion
InputStream is = getClass().getResourceAsStream("/explosion.mid");
playerExplosion = Manager.createPlayer(is, "audio/midi");
playerExplosion.realize();
playerExplosion.setLoopCount(1);
}
public void playExplosion() {
try {
System.out.println(playerExplosion.getState());
playerExplosion.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
MyMIDlet class
public class MyMIDlet extends MIDlet() {
public Sound sound;
public MyMIDlet() {
// Init sound object
try {
sound = new Sound(this);
} catch (Exception ex) {
ex.printStackTrace();
}
// Test
System.out.println("Start");
for (int i = 0; i < 5; i++) {
sound.playExplosion();
// My sound is less than 1 second.
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("End");
}
}
Output
Start
200
300
300
300
300
End
For the 1st time, playerExplosion state before start() is 200 (REALIZED).
For the 2nd, 3rd, 4th, 5th times, playerExplosion state before start() is 300 (PREFETCH).
At the moment, I have to create playerExplosion again in playExplosion() method, but it's too slow in a real device.
Upvotes: 4
Views: 516
Reputation: 95161
Realizing a Player can be a resource and time consuming process havint to create playerExplosion
would mean calling playerExplosion.realize();
again that is why the process takes so much time
When your player is in PREFETCHED State
what you need to do is just call playerExplosion.start();
but its not paying again because there is a general limitation on the number of times its should pay
Remove
playerExplosion.setLoopCount(1);
And you would be able to pay your file several times
Thanks
:)
Upvotes: 0