Guest User

Untitled

a guest
Jan 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. import java.io.InputStream;
  2. import sun.audio.*; //import the sun.audio package
  3. import java.io.*;
  4. import javax.sound.sampled.AudioInputStream;
  5. import javax.sound.sampled.AudioSystem;
  6. import javax.sound.sampled.Clip;
  7.  
  8. public class Main {
  9.  
  10. public Main() {
  11.  
  12. }
  13.  
  14. public static void main(String[] args) {
  15. System.out.println("hello there");
  16. playSound("Hi there");
  17.  
  18. }
  19.  
  20. public static synchronized void playSound(final String url) {
  21. new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
  22. // it blocks on the Clip finishing, see
  23. // comments
  24. public void run() {
  25. try {
  26. Clip clip = AudioSystem.getClip();
  27. AudioInputStream inputStream = AudioSystem
  28. .getAudioInputStream(Main.class
  29. .getResourceAsStream("pacman_chomp.wav"
  30. + url));
  31. clip.open(inputStream);
  32. clip.start();
  33. } catch (Exception e) {
  34. System.err.println(e.getMessage());
  35. }
  36. }
  37. }).start();
  38. }
  39.  
  40. }
  41.  
  42. AudioInputStream inputStream = AudioSystem
  43. .getAudioInputStream(Main.class
  44. .getResourceAsStream("pacman_chomp.wav"
  45. + url));
  46.  
  47. // First load the sound
  48. WavSound snd = WavPlayer.loadSound("pacman_chomp.wav");
  49. // play it then
  50. snd.play();
  51.  
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54.  
  55. import javax.sound.sampled.AudioFormat;
  56. import javax.sound.sampled.AudioInputStream;
  57. import javax.sound.sampled.AudioSystem;
  58. import javax.sound.sampled.DataLine;
  59. import javax.sound.sampled.LineUnavailableException;
  60. import javax.sound.sampled.SourceDataLine;
  61. import javax.sound.sampled.UnsupportedAudioFileException;
  62.  
  63. public class AePlayWave extends Thread {
  64.  
  65. protected static final boolean DEBUG = false;
  66.  
  67. protected InputStream inputStream;
  68.  
  69. public AePlayWave(InputStream inputStream) {
  70. this.inputStream = inputStream;
  71. if (DEBUG) System.out.println("AePlayWave constructor");
  72. }
  73.  
  74. @Override
  75. public void run() {
  76. if (DEBUG) System.out.println("AePlayWave running");
  77.  
  78. AudioInputStream audioInputStream = verifyInputStream();
  79. if (audioInputStream == null) {
  80. return;
  81. }
  82.  
  83. AudioFormat format = audioInputStream.getFormat();
  84. SourceDataLine audioLine = openInputStream(format);
  85.  
  86. if (DEBUG) System.out.println(audioLine.getLineInfo());
  87.  
  88. if (audioLine != null) {
  89. audioLine.start();
  90. playInputStream(audioInputStream, audioLine);
  91. }
  92. }
  93.  
  94. protected AudioInputStream verifyInputStream() {
  95. if (DEBUG) System.out.println("AePlayWave verifyInputStream");
  96. AudioInputStream audioInputStream = null;
  97. try {
  98. audioInputStream = AudioSystem.getAudioInputStream(inputStream);
  99. } catch (UnsupportedAudioFileException e) {
  100. e.printStackTrace();
  101. return null;
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. return null;
  105. }
  106. return audioInputStream;
  107. }
  108.  
  109. protected SourceDataLine openInputStream(AudioFormat format) {
  110. if (DEBUG) System.out.println("AePlayWave openInputStream");
  111. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  112. SourceDataLine audioLine = null;
  113. if (DEBUG) System.out.println("AePlayWave openInputStream try");
  114. try {
  115. audioLine = (SourceDataLine) AudioSystem.getLine(info);
  116. if (DEBUG) System.out.println("AePlayWave openInputStream getLine");
  117. audioLine.open(format);
  118. if (DEBUG) System.out.println("AePlayWave openInputStream open");
  119. } catch (LineUnavailableException e) {
  120. e.printStackTrace();
  121. return null;
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. return null;
  125. }
  126. return audioLine;
  127. }
  128.  
  129. protected void playInputStream(AudioInputStream audioInputStream,
  130. SourceDataLine audioLine) {
  131. if (DEBUG) System.out.println("AePlayWave playInputStream");
  132. int externalBufferSize = (int) audioInputStream.getFrameLength() * 4;
  133. if (DEBUG) System.out.println("AePlayWave playInputStream externalBufferSize: "
  134. + externalBufferSize);
  135. int nBytesRead = 0;
  136. byte[] abData = new byte[externalBufferSize];
  137.  
  138. try {
  139. while (nBytesRead != -1) {
  140. nBytesRead = audioInputStream.read(abData, 0, abData.length);
  141. if (nBytesRead >= 0) {
  142. if (DEBUG) System.out.println("Bytes read: " + nBytesRead);
  143. audioLine.write(abData, 0, nBytesRead);
  144. }
  145. }
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. return;
  149. } finally {
  150. audioLine.drain();
  151. audioLine.close();
  152. }
  153. }
  154. }
  155.  
  156. InputStream inputStream = getClass().getResourceAsStream("alarm-clock-1.wav");
  157. AePlayWave playWave = new AePlayWave(inputStream);
  158. playWave.run();
  159.  
  160. playSound("pacman_chomp.wav");
  161.  
  162.  
  163. public static synchronized void playSound(final String url) {
  164. new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
  165. // it blocks on the Clip finishing, see
  166. // comments
  167. public void run() {
  168. try {
  169. Clip clip = AudioSystem.getClip();
  170. AudioInputStream inputStream = AudioSystem
  171. .getAudioInputStream(Main.class
  172. .getResourceAsStream(url));
  173. clip.open(inputStream);
  174. clip.start();
  175. } catch (Exception e) {
  176. System.err.println(e.getMessage());
  177. }
  178. }
  179. }).start();
  180. }
  181.  
  182. playSound("pacman_chomp.wav");
  183. playSound("pacman_dies.wav");
Add Comment
Please, Sign In to add comment