25 Apr 2011

Source Code Membuat Musik Player dengan J2ME

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class Musikq extends MIDlet implements PlayerListener, CommandListener
{
 private Display dis;
 private static Player play;
 private VolumeControl vc;
 private List list;
 private Command CmdExit, CmdStop;

 public Musikq()
 {
  dis = Display.getDisplay(this);
 }

 public void startApp()
 {
  CmdExit = new Command("Exit", Command.EXIT, 1);
  CmdStop = new Command("Stop", Command.OK, 1);
  list = new List("Menu", List.IMPLICIT);
  list.append("xfiless.wav", null);
  list.append("Sapii.wav",null);
  list.append("opik.mid",null);
  list.addCommand(CmdExit);
  list.setCommandListener(this);
  dis.setCurrent(list);
 }

 public void pauseApp()
 {
 }

 public void destroyApp(boolean unconditional)
 {
  if(play != null)
  {
   play.close();
  }
 }

 public void commandAction(Command Cmd, Displayable s)
 {
  if (Cmd == List.SELECT_COMMAND)
  {
   switch (list.getSelectedIndex())
   {
case 0:
list.addCommand(CmdStop);
try
{
 playMedia("xfiless.wav", "wav");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
case 1:
list.addCommand(CmdStop);
try
{
playMedia("moo2.wav", "wav");
   }
   catch (Exception e)
   {
e.printStackTrace();
}
break;
case 2:
list.addCommand(CmdStop);
try
{
playMedia("opik.mid", "mid");
   }
   catch (Exception e)
   {
e.printStackTrace();
}
break;
}
}
  else if(Cmd == CmdExit)
  {
exitMIDlet();
  }
  else if(Cmd == CmdStop)
  {
   try
   {
play.stop();
if(play != null)
{
play.close();
}
   }
   catch(Exception e)
   {
   }
   list.removeCommand(CmdStop);
  }
}

public void exitMIDlet()
{
 destroyApp(false);
 notifyDestroyed();
 play = null;
}

private void playMedia(String file, String p)
throws Exception
{
 if(p.equals("wav"))
 {
  play = Manager.createPlayer(getClass().getResourceAsStream(file), "audio/x-wav");
 }
 else if(p.equals("mid"))
 {
  play = Manager.createPlayer(getClass().getResourceAsStream(file), "audio/midi");
 }
play.addPlayerListener(this);
play.setLoopCount(-1);
play.prefetch();
play.realize();
vc = (VolumeControl)play.getControl("VolumeControl");
if (vc != null)
  vc.setLevel(80);
play.start();
}

public void playerUpdate(Player play, String event, Object eventData)
{
 if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long)eventData))
  {
}
 else if(event.equals(PlayerListener.CLOSED))
 {
 }
}
};

2 komentar:

  1. makasih buwat source codenya...

    BalasHapus
  2. saya minta penjelasan dari source code RMS dong..
    tlng blz ea..?

    BalasHapus