Thursday, December 26, 2013

To Play *.wav sound file with Java

//to play *.wav sound file with java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JSound extends JFrame implements ActionListener
{
  JButton jb1;
  JPanel pane;
  public JSound()
  {
     setSize(300,200);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     jb1=new JButton("Play");
     jb1.addActionListener(this);
     pane=new JPanel();
     pane.add(jb1);
     setContentPane(pane);
     setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
     if(e.getSource()==jb1)
     {
       java.net.URL urlClick = JSound.class.getResource("hit.wav");
       //                         ^                        ^
       //                         |                        |
       //                     your class name         sound file
       //                                         in the same folder
       java.applet.Applet.newAudioClip(urlClick).play();
     }
  }
  public static void main(String args[])
  {
     new JSound();
  }
}

No comments:

Post a Comment