KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > audioaccessory


1 package com.ca.directory.jxplorer.editor;
2
3 // AudioAccessory.java
4
// A simple accessory for JFileChooser that lets you play .au clips.
5
//
6
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 JavaDoc;
12 import java.awt.*;
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.ActionListener JavaDoc;
15 import java.beans.PropertyChangeEvent JavaDoc;
16 import java.beans.PropertyChangeListener JavaDoc;
17 import java.io.File JavaDoc;
18 import java.net.URL JavaDoc;
19
20 public class audioaccessory extends JPanel implements PropertyChangeListener JavaDoc {
21
22   protected AudioClip currentClip;
23   protected String JavaDoc currentName="";
24   protected JLabel fileLabel;
25   protected CBButton playButton, stopButton, helpButton;
26   protected JOptionPane infoBox; // TE: information box.
27
protected boolean unSupported = false; //TE: used to check if an audio type is not supported (ie if it can be played).
28

29   public audioaccessory() {
30     // Set up the accessory. The file chooser will give us a reasonable size.
31
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 JavaDoc() {
43       public void actionPerformed(ActionEvent JavaDoc e) {
44         if (currentClip != null) {
45           if (unSupported){
46               infoBox = new JOptionPane(); //TE: information box added to inform user that certain types of audio can't be played.
47
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 JavaDoc() {
56       public void actionPerformed(ActionEvent JavaDoc e) {
57         if (currentClip != null) {
58           currentClip.stop();
59         }
60       }
61     });
62   }
63
64     public void propertyChange(PropertyChangeEvent JavaDoc e)
65     {
66         if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
67         {
68             unSupported = false;
69             // Ok, the user selected a file in the chooser
70
File JavaDoc f = (File JavaDoc)e.getNewValue();
71
72             if (f!=null)
73             {
74                 // Make reasonably sure it's an audio file
75
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                 { // TE makes sure that it is an audio file...these are just the ones that I have tested, there are sure to be more therefore this will need adjusting.
80
if (f.getName().toLowerCase().endsWith(".rmi") || f.getName().toLowerCase().endsWith(".ram") || f.getName().toLowerCase().endsWith(".mp3"))
81                     { //TE these are the audio types that are unsupported.
82
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 JavaDoc f) {
99     // Make sure we have a real file, otherwise, disable the buttons
100
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     // Ok, seems the audio file is real, so load it and enable the buttons
108
String JavaDoc name = f.getName();
109     if (name.equals(currentName)) {
110       return;
111     }
112     if (currentClip != null) { currentClip.stop(); }
113     currentName = name;
114     try {
115       URL JavaDoc u = f.getAbsoluteFile().toURL(); // CB try to make URL handling more robust...
116
//URL u = new URL("file:///" + f.getAbsolutePath());
117
currentClip = new AppletAudioClip(u);
118     }
119     catch (Exception JavaDoc 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  /**
130   * Stops the current audio clip
131   *
132   */

133   
134   public void stopPlay()
135   {
136     if(currentClip == null) {return;}
137     currentClip.stop();
138   }
139 }
Popular Tags