Saturday, April 5, 2014

Program Pemutar Musik di Pemrograman JAVA

Program Pemutar Musik di Pemrograman JAVA
Sintak Dalam Program
a.      canvasku.java
import javax.microedition.lcdui.*;
class canvasku extends Canvas implements CommandListener {
Command exitcommand,stop;
PlayAudioList midlet;
String text;
Image gb;
String [] gambar={"/macan.jpg","/gadis.jpg","/didi.jpg",
                  "/dEbit.jpg","/dIwan.jpg","/diwan2.jpg"};
String [] kata={"sample music wav",
                       "sample patern mid",
                       "stasiun balapan mp3 (didi kempot)",
                       "berita kepada kawan mp3 (ebiet g ade)",
                       "bento mp3 (iwan fals)",
                       "bongkar mp3 (iwan fals)"}; 
int pil;
public  canvasku(PlayAudioList satu, int jajal) {
pil=jajal;
midlet=satu;
exitcommand = new Command("Exit", Command.OK, 0);
stop=new Command("Stop", Command.OK, 1);
addCommand(exitcommand);
addCommand(stop);
setCommandListener(this);       
}
 public void paint(Graphics g) {   
            g.setColor(255, 255, 0 );
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(255,0, 0);
            g.drawString(kata[pil],getWidth()/2, getHeight()*4/5,
            Graphics.BOTTOM| Graphics.HCENTER);
            try {
            gb = Image.createImage(gambar[pil]);
            g.drawImage(gb, getWidth()/2, getHeight()*3/4,Graphics.BOTTOM|Graphics.HCENTER);
            } catch(Exception e) {}
        }

      public void commandAction(Command c, Displayable d) {
       if (c==exitcommand)midlet.Quit();
       else if (c==stop)midlet.Stop();
    }}


b.      PlayAudioList.java
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
public class PlayAudioList extends MIDlet implements CommandListener {
int p;
String [] lagu ={"music.wav","pattern.mid","Stasiun.mp3",
                 "Berita.mp3","Bento.mp3","Bongkar.mp3"};
VolumeControl vc;
Canvas cv;
Player player;
List lst;
Command mainkan;
String [] daftar={"1. sample music wav",
                  "2. sample patern mid",
                  "3. stasiun balapan mp3 (didi kempot)",
                  "4. berita kepada kawan mp3 (ebiet g ade)",
                  "5. bento mp3 (iwan fals)",
                  "6. bongkar mp3 (iwan fals)"};
String [] type ={"audio/x-wav","audio/midi","audio/mp3","audio/mp3","audio/mp3","audio/mp3"};

    public PlayAudioList(){
     lst =new List("DAFTAR LAGU", List.EXCLUSIVE);
     for(int i=0; i<daftar.length; i++){
     lst.append(daftar[i], null);}
     mainkan=new Command("Mainkan", Command.OK, 1);
     lst.addCommand(mainkan);
     lst.setCommandListener(this);
     p=lst.getSelectedIndex();
    }
      
    public void startApp() {
    Display.getDisplay(this).setCurrent(lst);
    } 
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) { }
    public void commandAction(Command c, Displayable d) {
    if(c==mainkan){
    p=lst.getSelectedIndex();
    MainkanAudio(p);
    cv =new canvasku(this,p);
    Display.getDisplay(this).setCurrent(cv);
    }
    }
        public void MainkanAudio(int pilih){
        try{
        InputStream is =getClass().getResourceAsStream(lagu[p]);                
        player = Manager.createPlayer(is,type[p]);
        player.realize();
        vc = (VolumeControl) player.getControl("VolumeControl");
        if(vc != null) { vc.setLevel(50); }
        player.start();
        }
        catch(Exception e){}
        }
    protected void Quit(){
    destroyApp(true);
    notifyDestroyed();
    }
    protected void Stop(){
    try {
    player.stop();
    } catch (MediaException ex) {
    ex.printStackTrace();
    }
    }
    }

hasil

Program Pemutar Musik di Pemrograman JAVA

No comments:

Post a Comment