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!
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!