1 package com.ca.directory.jxplorer.editor; 2 3 import com.ca.commons.cbutil.CBButton; 7 import com.ca.commons.cbutil.CBIntText; 8 import sun.applet.AppletAudioClip; 9 10 import javax.swing.*; 11 import java.applet.AudioClip ; 12 import java.awt.*; 13 import java.awt.event.ActionEvent ; 14 import java.awt.event.ActionListener ; 15 import java.beans.PropertyChangeEvent ; 16 import java.beans.PropertyChangeListener ; 17 import java.io.File ; 18 import java.net.URL ; 19 20 public class audioaccessory extends JPanel implements PropertyChangeListener { 21 22 protected AudioClip currentClip; 23 protected String currentName=""; 24 protected JLabel fileLabel; 25 protected CBButton playButton, stopButton, helpButton; 26 protected JOptionPane infoBox; protected boolean unSupported = false; 29 public audioaccessory() { 30 setLayout(new BorderLayout()); 32 add(fileLabel = new JLabel(" " + CBIntText.get("Select sound file")), BorderLayout.NORTH); 33 JPanel p = new JPanel(); 34 playButton = new CBButton(CBIntText.get("Play"), CBIntText.get("Play this audio clip.")); 35 stopButton = new CBButton(CBIntText.get("Stop"), CBIntText.get("Stop playing the current audio clip.")); 36 playButton.setEnabled(false); 37 stopButton.setEnabled(false); 38 p.add(playButton); 39 p.add(stopButton); 40 add(p, BorderLayout.CENTER); 41 42 playButton.addActionListener(new ActionListener () { 43 public void actionPerformed(ActionEvent e) { 44 if (currentClip != null) { 45 if (unSupported){ 46 infoBox = new JOptionPane(); infoBox.showMessageDialog(null, CBIntText.get("Unable to play audio formats of type .mp3, .rmi or .ram"), CBIntText.get("Information Message"), JOptionPane.INFORMATION_MESSAGE); 48 return; 49 } 50 currentClip.stop(); 51 currentClip.play(); 52 } 53 } 54 }); 55 stopButton.addActionListener(new ActionListener () { 56 public void actionPerformed(ActionEvent e) { 57 if (currentClip != null) { 58 currentClip.stop(); 59 } 60 } 61 }); 62 } 63 64 public void propertyChange(PropertyChangeEvent e) 65 { 66 if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) 67 { 68 unSupported = false; 69 File f = (File )e.getNewValue(); 71 72 if (f!=null) 73 { 74 if (f.getName().toLowerCase().endsWith(".mid") || f.getName().toLowerCase().endsWith(".aiff") 76 || f.getName().toLowerCase().endsWith(".wav") || f.getName().toLowerCase().endsWith(".au") 77 || f.getName().toLowerCase().endsWith(".rmi") || f.getName().toLowerCase().endsWith(".ram") 78 || f.getName().toLowerCase().endsWith(".mp3")) 79 { if (f.getName().toLowerCase().endsWith(".rmi") || f.getName().toLowerCase().endsWith(".ram") || f.getName().toLowerCase().endsWith(".mp3")) 81 { unSupported = true; 83 } 84 setCurrentClip(f); 85 } 86 else 87 { 88 setCurrentClip(null); 89 } 90 } 91 else 92 { 93 setCurrentClip(null); 94 } 95 } 96 } 97 98 public void setCurrentClip(File f) { 99 if ((f == null) || (f.getName() == null)) { 101 fileLabel.setText(" " + CBIntText.get("Select sound file")); 102 playButton.setEnabled(false); 103 stopButton.setEnabled(false); 104 return; 105 } 106 107 String name = f.getName(); 109 if (name.equals(currentName)) { 110 return; 111 } 112 if (currentClip != null) { currentClip.stop(); } 113 currentName = name; 114 try { 115 URL u = f.getAbsoluteFile().toURL(); currentClip = new AppletAudioClip(u); 118 } 119 catch (Exception e) { 120 e.printStackTrace(); 121 currentClip = null; 122 fileLabel.setText(CBIntText.get("Error loading clip.")); 123 } 124 fileLabel.setText(" " + name); 125 playButton.setEnabled(true); 126 stopButton.setEnabled(true); 127 } 128 129 133 134 public void stopPlay() 135 { 136 if(currentClip == null) {return;} 137 currentClip.stop(); 138 } 139 } | Popular Tags |