1 import java.net.URL ; 2 import javax.sound.sampled.*; 3 4 5 public final class Sound{ 6 7 private static final int BUFFER_SIZE=128000; 8 9 public Sound(){ 10 } 12 13 public void playOnlineSound(){ 14 String s=WhisperIM.UserPref.getProperty("sound_alert_online"); 15 if(s.equals("none") || s.equals("")){ 16 return; 17 } 18 URL u=null; 19 if(s.equals("default")){ 20 u=Icons.CL.getResource("sfx/online.wav"); 21 } 22 else{ 23 try{ 24 u=new URL ("file:///"+s); 25 } 26 catch(Exception e){ 27 } 29 } 30 playSound(u); 31 } 32 33 public void playNewChatSound(){ 34 String s=WhisperIM.UserPref.getProperty("sound_alert_new_chat"); 35 if(s.equals("none") || s.equals("")){ 36 return; 37 } 38 URL u=null; 39 if(s.equals("default")){ 40 u=Icons.CL.getResource("sfx/chat.wav"); 41 } 42 else{ 43 try{ 44 u=new URL ("file:///"+s); 45 } 46 catch(Exception e){ 47 } 49 } 50 playSound(u); 51 } 52 53 public void playNewMessageSound(){ 54 String s=WhisperIM.UserPref.getProperty("sound_alert_new_message"); 55 if(s.equals("none") || s.equals("")){ 56 return; 57 } 58 URL u=null; 59 if(s.equals("default")){ 60 u=Icons.CL.getResource("sfx/msg.wav"); 61 } 62 else{ 63 try{ 64 u=new URL ("file:///"+s); 65 } 66 catch(Exception e){ 67 } 69 } 70 playSound(u); 71 } 72 73 private void playSound(URL soundUrl){ 74 try{ 75 if(soundUrl==null){ 76 return; 77 } 78 PlayThread pt=new PlayThread(soundUrl); 79 pt.start(); 80 } catch(Exception e){ 82 e.printStackTrace(); 83 } 84 } 85 86 final class PlayThread extends Thread { 87 88 private URL url; 89 90 public PlayThread(URL 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 } catch(Exception e){ 118 } 120 } 121 } 122 } | Popular Tags |