KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Sound


1 import java.net.URL JavaDoc;
2 import javax.sound.sampled.*;
3
4 /** Plays sounds when messages are recieved, etc.*/
5 public final class Sound{
6     
7     private static final int BUFFER_SIZE=128000;
8     
9     public Sound(){
10         //nothing to do
11
}
12     
13     public void playOnlineSound(){
14         String JavaDoc s=WhisperIM.UserPref.getProperty("sound_alert_online");
15         if(s.equals("none") || s.equals("")){
16             return;
17         }
18         URL JavaDoc u=null;
19         if(s.equals("default")){
20             u=Icons.CL.getResource("sfx/online.wav");
21         }
22         else{
23             try{
24                 u=new URL JavaDoc("file:///"+s);
25             }
26             catch(Exception JavaDoc e){
27                 //swallow
28
}
29         }
30         playSound(u);
31     }
32     
33     public void playNewChatSound(){
34         String JavaDoc s=WhisperIM.UserPref.getProperty("sound_alert_new_chat");
35         if(s.equals("none") || s.equals("")){
36             return;
37         }
38         URL JavaDoc u=null;
39         if(s.equals("default")){
40             u=Icons.CL.getResource("sfx/chat.wav");
41         }
42         else{
43             try{
44                 u=new URL JavaDoc("file:///"+s);
45             }
46             catch(Exception JavaDoc e){
47                 //swallow
48
}
49         }
50         playSound(u);
51     }
52     
53     public void playNewMessageSound(){
54         String JavaDoc s=WhisperIM.UserPref.getProperty("sound_alert_new_message");
55         if(s.equals("none") || s.equals("")){
56             return;
57         }
58         URL JavaDoc u=null;
59         if(s.equals("default")){
60             u=Icons.CL.getResource("sfx/msg.wav");
61         }
62         else{
63             try{
64                 u=new URL JavaDoc("file:///"+s);
65             }
66             catch(Exception JavaDoc e){
67                 //swallow
68
}
69         }
70         playSound(u);
71     }
72     
73     private void playSound(URL JavaDoc soundUrl){
74         try{
75             if(soundUrl==null){
76                 return;
77             }
78             PlayThread pt=new PlayThread(soundUrl);
79             pt.start();
80         }// end try
81
catch(Exception JavaDoc e){
82             e.printStackTrace();
83         }
84     }
85     
86     final class PlayThread extends Thread JavaDoc{
87         
88         private URL JavaDoc url;
89         
90         public PlayThread(URL JavaDoc url){
91             super("Play Sound Thread");
92             setPriority(Thread.MIN_PRIORITY);
93             setDaemon(true);
94             this.url=url;
95         }
96         
97         public void run(){
98             try{
99                 AudioInputStream ais=AudioSystem.getAudioInputStream(url);
100                 AudioFormat af=ais.getFormat();
101                 DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
102                 SourceDataLine line=(SourceDataLine) AudioSystem.getLine(info);
103                 line.open(af);
104                 line.start();
105                 byte[] buffer=new byte[BUFFER_SIZE];
106                 int r=ais.read(buffer,0,BUFFER_SIZE);
107                 while(r!=-1){
108                     if(r>0){
109                         line.write(buffer,0,r);
110                     }
111                     r=ais.read(buffer,0,BUFFER_SIZE);
112                 }
113                 line.drain();
114                 line.close();
115                 
116             }//end try
117
catch(Exception JavaDoc e){
118                 //swallow
119
}
120         }
121     }
122 }
Popular Tags