Can I play any sound clip/background music for a J2ME game

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Can I play any sound clip/background music for a J2ME game

Postby koey » Mon Jan 22, 2007 6:09 pm

Sorry, I am a beginner of creating J2ME game, could anyone tell me how/ can I play any sound clip/background music for a J2ME game, if can, can you paste the code here for reference. Many thanks!
koey
 
Posts: 44
Joined: Fri Jan 19, 2007 5:07 pm

Postby DevelopmentTeam » Tue Jan 23, 2007 4:02 am

You can use this class in your code to play a midi file.

Code: Select all
public class PlayMid implements Runnable{
    private Player p;
    private Thread soundThread;
   
    public PlayMid(String menuUrl, byte loopCount, String type, byte volumeLevel) {
        p = createPlayer(menuUrl,"audio/" + type, loopCount);
        VolumeControl vc = (VolumeControl) p.getControl("VolumeControl");
        if(vc != null)
            vc.setLevel(volumeLevel * 10);
    }
   
    public void stopSound() {
        try {
            p.stop();
            soundThread = null;
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
   
    public void startSound() {
        stopSound();
        soundThread = new Thread(this);
        soundThread.start();
    }
   
    private Player createPlayer(String filename,String format, byte loopCount) {
        Player p = null;
        try {
            InputStream is = getClass().getResourceAsStream(filename);
            p = Manager.createPlayer(is,format);
            p.setLoopCount(loopCount);
            p.prefetch();
        } catch(Exception e) {
        }
        return p;
    }
   
    public void run(){
        if(p != null) {
            try {
                p.stop();
                p.start();
            } catch(Exception e) {
            }
        }
    }
}
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Postby koey » Tue Jan 23, 2007 3:44 pm

Thank you very much! Let me try to integrate the code to my program. Thanks!
koey
 
Posts: 44
Joined: Fri Jan 19, 2007 5:07 pm


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron