Advertisement
Guest User

JavaSound MWE

a guest
May 17th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. package com.example.project.name;
  2.  
  3. import java.net.URL;
  4.  
  5. import javax.sound.sampled.*;
  6.  
  7. public class TestClass {
  8.    
  9.     public static void main (String[] args) {
  10.        
  11.         new TestClass().run();
  12.        
  13.     }
  14.    
  15.     public void run () {
  16.        
  17.         Clip clip = null;
  18.         ClassLoader cl = this.getClass().getClassLoader();
  19.         AudioInputStream ais;
  20.         URL url = cl.getResource("com/example/project/assets/TestSound.wav");
  21.         System.out.println(url);
  22.         try {
  23.             ais = AudioSystem.getAudioInputStream(url);
  24.             System.out.println(ais);
  25.             clip = AudioSystem.getClip();
  26.             System.out.println(clip);
  27.             clip.open(ais);
  28.             System.out.println(clip.isOpen());
  29.            
  30.         }
  31.         catch (Exception e) {
  32.            
  33.             e.printStackTrace();
  34.             System.exit(1);
  35.            
  36.         }
  37.        
  38.         clip.start();
  39.         System.out.println(clip.isRunning());
  40.        
  41.         try {
  42.             Thread.sleep(100);
  43.             while (clip.isRunning()) {
  44.                 Thread.sleep(100);
  45.             }
  46.         }
  47.         catch (Exception e) {
  48.            
  49.             e.printStackTrace();
  50.         }
  51.        
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement