Skip to main content

Javax Code for Playing Music:

Javax Code for Playing Music:



OUTPUT:








________________________________________________________________ Just Copy and Paste and Change the variable name respectively:
________________________________________________________________
________________________________________________________________
Buttons:
1. jButton1 : Open Music File
2. jButton2 : Play
3, jButton3 : Stop
4. jButton4 : Pause
________________________________________________________________

import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;


public class Music extends javax.swing.JFrame {

    /** Creates new form Music */
    public Music() {
        initComponents();
    }
private Clip clip;
int pos;
    /** 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() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Open Music File ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Play");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Stop");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Pause");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(72, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(124, 124, 124))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(jButton4)))
                .addContainerGap(193, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
String fileName = "c:\\a.wav";
        try {
            File file = new File(fileName);
            if (file.exists()) {
                AudioInputStream sound = AudioSystem.getAudioInputStream(file);
             // load the sound into memory (a Clip)
                clip = AudioSystem.getClip();
                clip.open(sound);
            }
            else {
                throw new RuntimeException("Sound: file not found: " + fileName);
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: " + e);
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: " + e);
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: " + e);
        }
        catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
        }




        
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

         clip.setFramePosition(0);  // Must always rewind!
        clip.start();
        jButton4.setText("Pause");



    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
         clip.stop();

    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (jButton4.getText()== "Pause") {
                  pos = clip.getFramePosition();
                  clip.stop();
                  jButton4.setText("Play");
        }
        else{
                    jButton4.setText("Pause");
                    clip.setFramePosition(pos);
                    clip.start();
        }

    }                                        

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    // End of variables declaration                   

}

Comments

  1. Dont tell me Sumit ki tune ye code khud likha .....btw good job :)

    ReplyDelete
    Replies
    1. ofcourse not.. cant you see the screenshot of the window is from Windows XP. My other window screenshots are from Windows 7.

      Delete
  2. Wow... It helped me so much to do my final year assingment thank you..

    ReplyDelete

Post a Comment

Popular posts from this blog

My Educational Philosophy in a Nutshell - Hareshwar P. Singh

My Educational Philosophy in a Nutshell Author: Hareshwar P Singh Education, in a wider perspective, is not merely the accumulation of knowledge and information in various academic disciplines, which is nevertheless essential in order to earn one’s living and survive in an increasingly competitive world. In its holistic sense, education is a means of total development of a pupil—physical, intellectual and emotional/spiritual development. True education must lead one from knowledge to wisdom, and ultimately to a profound sense of intelligence and enlightenment. Such an educated person has a great insight not only into the way human mind works but also into the ways of the world, and therefore is capable of living sanely in this seemingly insane world. Taking a more pragmatic view, it is the function or role of education to keep society supplied with various kinds of skilled professionals needed for the smooth functioning of a nation. In the contemporary educational landscape, ...

Get Pascal's Triangle - Netbeans Java Codes

Run-Shot of Pascal's Triangle Java Code: public class Pascalstriangle { public static void main(String args[]) { int x = 6; int triangle[][] = new int[x][x]; for (int i = 0; i < x; i++) { for (int j = 0; j < x; j++) { triangle[i][j] = 0; } } for(int i = 0; i < x; i++) { triangle[i][0] = 1 ; } for (int i = 1; i < x; i++) { for (int j = 1; j < x; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } } for (int i = 0; i < x; i++) { for(int j=0;j<=i;j++) { System.out.print(triangle[i][j]+ " "); } System.out.println(); } } } [Find more Netbeans Java Code at www.sumikuma.tk] You can also download text file version of this code: Pascal-triangle_javacode.txt