1 29 30 import java.io.*; 31 import javax.sound.sampled.*; 32 import java.util.jar.*; 33 34 35 50 51 public class AudioFilePlayer 52 { 53 54 private String soundFileName, jarFileName; 55 56 57 61 62 public AudioFilePlayer(String soundFileName) 63 { 64 this.soundFileName = soundFileName; 65 } 66 67 68 69 73 74 public AudioFilePlayer(String jarFileName, String soundFileName) 75 { 76 this.jarFileName = jarFileName; 77 this.soundFileName = soundFileName; 78 } 79 80 81 82 85 86 public void playFromJarFile() 87 { 88 89 try 90 { 91 JarFile thisJarFile = new JarFile(jarFileName); 92 93 JarEntry audioEntry = thisJarFile.getJarEntry(soundFileName); 94 95 BufferedInputStream jarFileInputStream = new BufferedInputStream(thisJarFile.getInputStream(audioEntry)); 97 98 AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(jarFileInputStream); 100 101 104 AudioFormat audioFormat = new AudioFormat(11025, 16, 1, true, false); 106 107 DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat); 108 109 Clip audioClip = (Clip)AudioSystem.getLine(dataLineInfo); 110 111 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(jarFileInputStream); 112 113 audioClip.open(audioInputStream); 114 115 audioClip.start(); 116 } 117 catch (Exception e) 118 { 119 System.out.println(e); 121 } 122 123 } 124 125 126 127 130 131 public void playFromFile() 132 { 133 134 try 135 { 136 File audioFile = new File(soundFileName); 137 138 AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(audioFile); 139 140 AudioFormat audioFormat = audioFileFormat.getFormat(); 141 142 DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat); 143 144 Clip audioClip = (Clip)AudioSystem.getLine(dataLineInfo); 145 146 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile); 147 148 audioClip.open(audioInputStream); 149 150 audioClip.start(); 151 } 152 catch (Exception e) 153 { 154 System.out.println(e); 156 } 157 158 } 159 160 } | Popular Tags |