Java - Mini Lettore Wav


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package Multimedia;
  2.  
  3. import javax.swing.JDialog;
  4. import javax.swing.JFrame;
  5. import javax.swing.UIManager;
  6. import javax.swing.UnsupportedLookAndFeelException;
  7. import javax.swing.plaf.metal.MetalLookAndFeel;
  8.  
  9. public class AudioSystem
  10. {
  11. public static void main(String[] args)
  12. {
  13. JFrame.setDefaultLookAndFeelDecorated(true);
  14. JDialog.setDefaultLookAndFeelDecorated(true);
  15.  
  16. try
  17. {
  18. UIManager.setLookAndFeel(new MetalLookAndFeel());
  19. }
  20. {
  21. e.printStackTrace();
  22. }
  23.  
  24. new AudioGUI();
  25. }
  26. }
  27.  
  28.  
  29. package Multimedia;
  30.  
  31. import java.awt.Container;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.WindowEvent;
  35. import java.awt.event.WindowListener;
  36. import java.io.File;
  37.  
  38. import javax.swing.JButton;
  39. import javax.swing.JFileChooser;
  40. import javax.swing.JFrame;
  41. import javax.swing.JPanel;
  42.  
  43. public class AudioGUI extends JFrame implements WindowListener
  44. {
  45. public AudioGUI()
  46. {
  47. this.setTitle("AudioSystem");
  48. this.setSize(240, 100);
  49. this.setResizable(false);
  50.  
  51. this.addWindowListener(this);
  52.  
  53. Container gc = this.getContentPane();
  54. gc.add(new AudioGUIPan());
  55.  
  56. this.setVisible(true);
  57. }
  58.  
  59. public void windowOpened(WindowEvent e)
  60. {
  61.  
  62. }
  63.  
  64. public void windowClosing(WindowEvent e)
  65. {
  66. System.exit(0);
  67. }
  68.  
  69. public void windowClosed(WindowEvent e)
  70. {
  71.  
  72. }
  73.  
  74. public void windowIconified(WindowEvent e)
  75. {
  76.  
  77. }
  78.  
  79. public void windowDeiconified(WindowEvent e)
  80. {
  81.  
  82. }
  83.  
  84. public void windowActivated(WindowEvent e)
  85. {
  86.  
  87. }
  88.  
  89. public void windowDeactivated(WindowEvent e)
  90. {
  91.  
  92. }
  93. }
  94.  
  95. class AudioGUIPan extends JPanel implements ActionListener
  96. {
  97. private JButton play;
  98. private JButton stop;
  99. private JButton loop;
  100. private JButton open;
  101. private JButton close;
  102.  
  103. private JFileChooser jfc;
  104.  
  105. private String file;
  106.  
  107. private AudioDevice ad;
  108.  
  109. public AudioGUIPan()
  110. {
  111. play = new JButton("Play");
  112. play.addActionListener(this);
  113. stop = new JButton("Stop");
  114. stop.addActionListener(this);
  115. open = new JButton("Open");
  116. open.addActionListener(this);
  117. close = new JButton("Close");
  118. close.addActionListener(this);
  119.  
  120. this.add(stop);
  121. this.add(play);
  122. this.add(open);
  123. this.add(close);
  124. }
  125.  
  126. public void actionPerformed(ActionEvent e)
  127. {
  128. if(e.getSource() == close)
  129. {
  130. System.exit(0);
  131. }
  132. else if(e.getSource() == open)
  133. {
  134. jfc = new JFileChooser();
  135. jfc.setFileFilter(new AudioFilter());
  136.  
  137. if(jfc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
  138. {
  139. file = jfc.getSelectedFile().getAbsolutePath();
  140. ad = new AudioDevice(new File(file));
  141. }
  142. }
  143. else if(e.getSource() == play)
  144. {
  145. if(ad != null)
  146. ad.play();
  147. }
  148. else if(e.getSource() == stop)
  149. {
  150. if(ad != null)
  151. ad.stop();
  152. }
  153. }
  154. }
  155.  
  156.  
  157. package Multimedia;
  158. import java.io.File;
  159.  
  160. import javax.swing.filechooser.FileFilter;
  161.  
  162. public class AudioFilter extends FileFilter
  163. {
  164. public boolean accept(File f)
  165. {
  166. return f.getName().toLowerCase().endsWith(".wav") || f.isDirectory();
  167. }
  168.  
  169. public String getDescription()
  170. {
  171. return "Audio File *.wav";
  172. }
  173. }
  174.  
  175.  
  176. package Multimedia;
  177.  
  178. import java.applet.Applet;
  179. import java.applet.AudioClip;
  180. import java.io.File;
  181. import java.net.MalformedURLException;
  182.  
  183. public class AudioDevice implements AudioClip
  184. {
  185. private AudioClip ac;
  186. public AudioDevice(File f)
  187. {
  188. try
  189. {
  190. ac = Applet.newAudioClip(f.toURL());
  191. }
  192. {
  193. e.printStackTrace();
  194. }
  195. }
  196.  
  197. public void play()
  198. {
  199. ac.play();
  200. }
  201.  
  202. public void stop()
  203. {
  204. ac.stop();
  205. }
  206.  
  207. public void loop()
  208. {
  209.  
  210. }
  211. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.