Rabu, 17 Agustus 2011

Creating a Form Music In Netbeans

First, Java has limited playing music. The applet class in java is only capable of playing 5 types of voice-only ie AU, AIF, RMF, MIDI and WAV. But java is now capable of playing all types of music and videos, via the JMF class. In the discussion this time, only to be discussed in the playing of musical ability java AU, AIF, RMF, MIDI and WAV only, given the JMF classes need a more detailed discussion.

Class to handle the music found on:
   import java.applet.Applet; to call the function from the class Applet
   import java.applet.AudioClip; to call the function from the class AudioClip
   import java.net.URL; to get the URL (the path of the file) and the exception of the URL.


For the record, the java.applet.Applet; to initialize / instace to the music of a URL usually wear: Applet. newAudioClip (URL)

Method of AudioClip are:
     play (): play the sound only once
     loop (): plays the sound repeatedly
     stop (): turn off the sound.


Example Applications:
There is a Form with a TextField (eLokasi), 3 Button (bPlay, bBerulang and bStop) and 1 label. In the game / games, usually live music is played, the first time the form is run, or when a button is pressed. This form will behave the same, ie, when it was first played seubah will play a WAV file repeatedly (looping). When the Stop button is pressed, the music stops.
When the TextField eLokasi filled by a file path, then pressed the button Play1x (bPlay), or the Berulang2x (bBerulang), then the music will sound again, until the Stop button is pressed again.






  












The main class with the name formMusik.java 


/**
 *
 * @author nEGa
 */
public class formMusik extends javax.swing.JFrame {


    /** Creates new form formMusik */
    public formMusik() {
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtFile = new javax.swing.JTextField();
        bStop = new javax.swing.JButton();
        bPlay = new javax.swing.JButton();
        bLoop = new javax.swing.JButton();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Form Musik");


        jPanel1.setBackground(new java.awt.Color(0, 102, 204));
        jPanel1.setLayout(null);


        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 204, 0));
        jLabel1.setText("Lokasi dan Nama File :");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 160, 20);
        jPanel1.add(txtFile);
        txtFile.setBounds(10, 30, 370, 20);


        bStop.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bStop.setText("Stop");
        bStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bStopActionPerformed(evt);
            }
        });
        jPanel1.add(bStop);
        bStop.setBounds(10, 100, 370, 23);


        bPlay.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bPlay.setText("Play");
        bPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPlayActionPerformed(evt);
            }
        });
        jPanel1.add(bPlay);
        bPlay.setBounds(10, 70, 180, 23);


        bLoop.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bLoop.setText("Play Berulang");
        bLoop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoopActionPerformed(evt);
            }
        });
        jPanel1.add(bLoop);
        bLoop.setBounds(200, 70, 180, 23);


        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);


        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-409)/2, (screenSize.height-174)/2, 409, 174);
    }// </editor-fold>

    private void bPlayActionPerformed(java.awt.event.ActionEvent evt) {
        cs.bunyi();
    }


    private void bLoopActionPerformed(java.awt.event.ActionEvent evt) {
        cs.ulang();
    }


    private void bStopActionPerformed(java.awt.event.ActionEvent evt) {
        cs.berhenti();
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new formMusik().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify
    private javax.swing.JButton bLoop;
    private javax.swing.JButton bPlay;
    private javax.swing.JButton bStop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtFile;
    // End of variables declaration
}



Pembuatan class pemutar musik :

import java.applet.AudioClip;
import java.net.URL;
import javax.swing.JOptionPane;
/**
 *
 * @author SBU
 */
public class cariSuara {
    AudioClip sound;
    public cariSuara(String suara){
        try {
            fAudio = new URL(suara);
            sound = Applet.newAudioClip(fAudio);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Suara di folder "+suara+" tidak ada...!!!");
        }
    }

    public void bunyi(){
        sound.play();
    }
    public void berhenti(){
        sound.stop();
    }
    public void ulang(){
        sound.loop();
    }
}

 

0 komentar:

Posting Komentar