re-playing sound in J2ME

M

marko

Hi,
I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
I used this method to start playing sound:

public void playsnd() {
try {
InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");
p.prefetch();
p.start();
}
catch (IOException ex) {}
catch (MediaException ex) {}

}

This method works fine except it has a long delay before sound actually
starts playing.
Is it possible to initialize and create player in class constructor and
then call start() method on player when needed?
I tried this but it gives me null pointer exception when I call
startplaying() method:


import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.Random;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;

public class ExampleGameCanvas extends GameCanvas implements Runnable {

private Player p;

public ExampleGameCanvas() throws Exception {

InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");
p.prefetch();

}

public void startplaying() {

try {
p.start();
}
catch (MediaException ex) {}

}

}


Thank You!
 
D

Darryl L. Pierce

marko said:
Hi,
I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
I used this method to start playing sound:

public void playsnd() {
try {
InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");
p.prefetch();
p.start();

You don't need to prefetch the sound file. When you call start() it
fetches it then and it takes just as long as if you called prefetch().
}
catch (IOException ex) {}
catch (MediaException ex) {}

}

This method works fine except it has a long delay before sound actually
starts playing.

That's the time the system has to spend actually loading the data from
the input stream. Believe it or not, it can be quite slow reading data
from the JAR.
Is it possible to initialize and create player in class constructor and
then call start() method on player when needed?

Yes, you can instantiate the player without calling start().
I tried this but it gives me null pointer exception when I call
startplaying() method:

That's because you've declared an instance variable:
private Player p;

But then in your constructor you created a local variable with the same
name:
public ExampleGameCanvas() throws Exception {

InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");

Here you should instead be doing:

p = Manager.createPlayer(is,"audio/x-wav");

And that will fix *this* problem.
 
M

marko

That's because you've declared an instance variable:
But then in your constructor you created a local variable with the same
name:



Here you should instead be doing:

p = Manager.createPlayer(is,"audio/x-wav");

And that will fix *this* problem.

Yes, that solved the problem and now it works without any delay! THANK
YOU very much :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top