Monday 21 July 2014

program to play music in JFrame

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
 
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class sounds extends JFrame {
 
   // Constructor
   public sounds() {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Test Sound Clip");
      this.setSize(300, 200);
      this.setVisible(true);
   
      try {
   
         // Open an audio input stream.
         URL url = this.getClass().getClassLoader().getResource("mus.wav");
         //
          AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
         // Get a sound clip resource.
          for(int i=0; i<10; i++){   Clip clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioIn);
         clip.start();}
      } catch (UnsupportedAudioFileException e) {
     JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      } catch (LineUnavailableException e) { JOptionPane.showMessageDialog(this, e.getMessage());
         e.printStackTrace();
      }
   }
 
   public static void main(String[] args) {
      new sounds();
   }
}

No comments:

Post a Comment

SAY HELLO!!