
import javax.microedition.media.*;
import java.io.*;

class SoundPlayer {

    private static SoundPlayer instance;
    Player p = null;
    private Player palyerFire;
    private Player playerExplode;
    private Player playerBg;
    private Player playerMove;
    private Player playerRoger;
    private Player playerYessir;
    private Player playerYeah;
    private Player playerEat;

    SoundPlayer() {
    }

    static SoundPlayer getInstance() {
        if (instance == null) {
            instance = new SoundPlayer();
        }
        return instance;
    }

    void playFireSound() {
        if (palyerFire == null) {
            palyerFire = createPlayer("/fire.wav", "audio/x-wav");
        }
        startPlayer(palyerFire);
    }

    void playExplodeSound() {
        if (playerExplode == null) {
            playerExplode = createPlayer("/explode.wav", "audio/x-wav");
        }
        startPlayer(playerExplode);
    }

    void playBgSound() {
        if (playerBg == null) {
            playerBg = createPlayer("/bg.wav", "audio/x-wav");
        }
        startPlayer(playerBg);
    }

    void playMoveSound() {
        if (playerMove == null) {
            playerMove = createPlayer("/move.wav", "audio/x-wav");
        }
        startPlayer(playerMove);
    }

    void playRogerSound() {
        if (playerRoger == null) {
            playerRoger = createPlayer("/roger.wav", "audio/x-wav");
        }
        startPlayer(playerRoger);
    }

    void playYessirSound() {
        if (playerYessir == null) {
            playerYessir = createPlayer("/yessir.wav", "audio/x-wav");
        }
        startPlayer(playerYessir);
    }

    void playYeahSound() {
        if (playerYeah == null) {
            playerYeah = createPlayer("/yeah.wav", "audio/x-wav");
        }
        startPlayer(playerYeah);
    }

    void playEatSound() {
        if (playerEat == null) {
            playerEat = createPlayer("/eat.wav", "audio/x-wav");
        }
        startPlayer(playerEat);
    }

    private void startPlayer(Player p) {
        if (p != null) {
            try {
                p.stop();
                p.setMediaTime(0L);
                p.start();
            } catch (MediaException me) {
                System.out.println(me);
            }
        }
    }

    private Player createPlayer(String filename, String format) {

        try {
            InputStream is = getClass().getResourceAsStream(filename);
            p = Manager.createPlayer(is, format);
            p.prefetch();
        } catch (IOException ioe) {
            System.out.println(ioe);
        } catch (MediaException me) {
        }
        return p;
    }

    public void stopSound() {
        try {
            p.stop();
            if (palyerFire != null) {
                palyerFire.stop();
            }
            if (playerExplode != null) {
                playerExplode.stop();
            }
            if (playerBg != null) {
                playerBg.stop();
            }
            if (playerMove != null) {
                playerMove.stop();
            }
            if (playerRoger != null) {
                playerRoger.stop();
            }
            if (playerYessir != null) {
                playerYessir.stop();
            }
            if (playerYeah != null) {
                playerYeah.stop();
            }
            if (playerEat != null) {
                playerEat.stop();
            }
        } catch (MediaException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}