Sound in a Game!
Hi guys!
I'm writing a game for Sony Ericsson K700.
And i have a problem with a sound. Also i'm working with MIDP 2.0.
My problem is to play 2 sounds in a game. When i play first sound, it play good, then is playing second sound and i have exception.
However, can somebody write an example, how to play several sounds in a game.
Thanks!
[375 byte] By [
Solnushkoa] at [2007-11-27 10:20:52]

# 1
did you play sounds in differents threads?
# 2
No, sounds plays in some thread...
Also, here is my class
import javax.microedition.media.*;
import java.io.InputStream;
import java.io.IOException;
public class PlayerManager implements Runnable{
private static Player lose;
private static Player win;
private static InputStream is;
private static PlayerManager instance;
private Thread thread;
public static final byte WIN = 1;
public static final byte LOSE = 0;
public static final byte ALL = 2;
public static byte win_state = 0;
public static PlayerManager getInstance(){
if (instance == null) instance = new PlayerManager();
return instance;
}
public synchronized void stopSound(byte sound){
try{
switch (sound){
case WIN:{
win.stop();
win.deallocate();
win.setMediaTime(0);
break;}
case LOSE:{
lose.stop();
lose.deallocate();
lose.setMediaTime(0);
break;}
case ALL:{
lose.stop();
lose.deallocate();
lose.setMediaTime(0);
win.stop();
win.deallocate();
win.setMediaTime(0);
break;}
}
} catch (MediaException me){}
}
private synchronized void startSound(byte sound){
try{
switch(sound){
case LOSE:{
lose.prefetch();
lose.start();
break;}
case WIN:{
win.prefetch();
win.start();
break;}
}
} catch (MediaException me){System.out.println("Media Exception in <<startSound()>>");}
}
public void loadData(){
try{
is = getClass().getResourceAsStream("/win.mid");
win = Manager.createPlayer(is, "audio/midi");
win.realize();
is = getClass().getResourceAsStream("/lose.mid");
lose = Manager.createPlayer(is, "audio/midi");
lose.realize();
} catch (MediaException mex){System.out.println("MediaException in <<loadData>>");
} catch (IOException ioe){System.out.println("IOException in <<loaddata>>");}
Setting.levelLoad += 10;
}
public void start(){
thread = new Thread(this);
thread.start();
}
public void run(){
switch(win_state){
case 1: startSound(WIN);
break;
case 0: startSound(LOSE);
break;
}
}
}
And now i have another problem, in my game play just one sound, second was never played. It's not writed any exception...
And i can't understend why...
# 3
If you can, write please an little example :)
# 4
private void playSound(String sound){
Thread t=new Thread(new Runnable(){
public void run() {
// creates player and play sound here
}
});
t.start();
}
or
public class SoundPlayer extends Thread{
Player player=null;
public SoundPlayer(String sound){
// constructor code ...
// creates player
}
public void run() {
//play sound here
}
}
# 5
Ok, thanks!
But what is wrong in my code?
I understend where i must create Player,
but problem is by prefetch() && close() && realize() methods.
Have you any working class with sound implementing?
I have to look, in my class sound s plays each in own thread...
Please, tell me, what have i wrong to do... thanks!
# 6
try this...
private void playSound(String sound){
Thread t=new Thread(new Runnable(){
public void run() {
try {
InputStream is = getClass().getResourceAsStream(sound);
Player p = Manager.createPlayer(is, "audio/mid");
p.start();
} catch (MediaException pe) {
pe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
};
t.start();
tell me if it's working
# 7
Hi, supareno!
Thank you for your answers! I see that this part of code will work.
But if you load every time ressource, that will slow down a game...
And i have to find another solution. I don't know, is it right or no. Look:
import javax.microedition.media.*;
import java.io.InputStream;
import java.io.IOException;
public class PlayerManager implements Runnable{
private static Player player;
private static InputStream win, lose, curent;
private static PlayerManager instance;
private Thread thread;
public static final byte WIN = 1;
public static final byte LOSE = 0;
public static byte win_state = 0;// выигрыш или проигрыш
public static PlayerManager getInstance(){
if (instance == null) instance = new PlayerManager();
return instance;
}
public void loadSound(){
win = getClass().getResourceAsStream("/win.mid");
lose = getClass().getResourceAsStream("/lose.mid");
try{
player = Manager.createPlayer(win,"audio/midi");
player.prefetch();
defplayer(player);
player = Manager.createPlayer(lose,"audio/midi");
player.prefetch();
defplayer(player);
} catch (IOException ioe){System.out.println("IOException in <<loadSound>>");
} catch (MediaException me){System.out.println("MediaException in <<loadSound>>");}
}
public synchronized void stopSound(){
try{
defplayer(player);
} catch (Throwable th){
reset(player);
}
}
private synchronized void startSound(byte sound){
String filename;
switch(sound){
case LOSE: filename = "/lose.mid"; break;
default: filename = "/win.mid"; break;
}
try{
defplayer(player);
} catch (Throwable th){System.out.println("Throwable in startSound");}
try{
curent = getClass().getResourceAsStream(filename);
player = Manager.createPlayer(curent, "audio/midi");
player.prefetch();
player.start();
} catch (IOException ioe){System.out.println("IOException in <<startSound>>");
} catch (MediaException me){System.out.println("MediaException in <<startSound>>");
}}
private void defplayer(Player player) throws MediaException {
if (player != null) {
if(player.getState() == Player.STARTED) {
player.stop();
}
if(player.getState() == Player.PREFETCHED) {
player.deallocate();
}
if(player.getState() == Player.REALIZED ||
player.getState() == Player.UNREALIZED) {
player.close();
}
}
player = null;
}
private void reset(Player player){
player = null;
}
public void start(){
thread = new Thread(this);
thread.start();
}
public void run(){
switch(win_state){
case WIN: startSound(WIN);
break;
case LOSE: startSound(LOSE);
break;
}
}
}
First of all i load ressources & call prefetch() for loading all ressources in a memory (i think so :). Then i destroy res. & load another res, becouse 2 players can't to be in PREFETCHED state. This all is realized in loadSound() method. And this is in my opinion problems part of code. However tell me please, what you mean... :)
# 8
http://www.blackberry.com/developers/docs/4.1api/javax/microedition/media/doc-files/states.gif
according to this scheme, when the sound is finished, it goes to PREFETCHED state so...
the best way is to download a javame game with sound and see who they done the stuff !!!