1

//This AudioTest class is to use clip to play music

    import java.io.IOException;
    import javax.sound.sampled.*;

    class AudioTest {

        public void AudioPlay() {

        ClassLoader classLoader = 

Thread.currentThread().getContextClassLoader();

        try{
          Clip clip = AudioSystem.getClip();
          AudioInputStream inputStream = 
AudioSystem.getAudioInputStream(
classLoader.getResourceAsStream("audio1.wav"));

           clip.open(inputStream);
           clip.start();
           while (!clip.isRunning())
               Thread.sleep(10);
           while (clip.isRunning())
               Thread.sleep(10);
           clip.close();
          } catch (Exception e)
            {
             System.out.println("Something failed");
            }
        //to see if the sound is finished playing 
        System.out.println("done");
        } 
    }

public class Test11 {

       public static void main(String[] args)
       throws IOException
       {
        AudioTest au1 = new AudioTest();
        au1.AudioPlay();
       }

    }

// The above Test11 class works fine in playing .wav music file. But in Test15 class I put this file together with another thread and executed it, it showed up "Something filed" message for playing audio1.wav music file.

//The multi-thread coding, Test15, is listed below:

    import java.io.IOException;
    import javax.sound.sampled.*;

//The following AudioTest class is exactly the same as that in the previous coding

    class AudioTest {

      public void AudioPlay() {

        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
        try{
              Clip clip = AudioSystem.getClip();
              AudioInputStream inputStream = 
              AudioSystem.getAudioInputStream(
              classLoader.getResourceAsStream(
              "audio1.wav"));
              clip.open(inputStream);
              clip.start();
              while (!clip.isRunning())
                      Thread.sleep(0);
              while (clip.isRunning())
                      Thread.sleep(0);
              clip.close();
            } catch (Exception e)
              {
               System.out.println("something failed");
              }

        System.out.println("done"); 
        }
    }

    class ThreadDemo extends Thread {
       private Thread t;
       private String threadName;

       ThreadDemo( String name) {
          threadName = name;
          System.out.println("Creating " +  threadName );
       }

       public void run() {
         //System.out.println("Running " +  threadName );
       try {
           if (threadName == "Thread-1") {
               System.out.println("Thread-1 is been started.");  
           // Start to play .wav file here
           AudioTest au1 = new AudioTest();
                au1.AudioPlay();
           }  
           else if (threadName == "Thread-2") {
               for (int i = 0; i < 100; i ++) {
                   System.out.println(i + ": " +  threadName + " is been executed."); 
               }
           }
      }catch (Exception e) {
         System.out.println("Thread " +  threadName + " failed.");
      }
   }

   public void start () {
      System.out.println("Starting " +  threadName );
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}

public class Test15 {
   public static void main(String args[]) throws IOException
   {
      ThreadDemo T1 = new ThreadDemo("Thread-1");
      T1.start();

      ThreadDemo T2 = new ThreadDemo("Thread-2");
      T2.start();
   }   
 }

//The above Class Test15 could be run but Thread-1 could only be started without playing any music and "something failed." showed. It looks Thread-2 was running OK.

//My ultimate goal is to show a moving score accompany with its matching music, audio.wav. If Thread-1 in Test15 is working, then I will add the moving score coding as Thread-2 in my code. If you are interested, I can sent you this moving-score coding in Stackoverflow or by email: dienfoonwu@gmail.com

Dien
  • 33
  • 6
  • 1
    You should print out the contents of the `Exception e` so you can find out what actually failed. – Ken Y-N Feb 17 '20 at 04:11
  • How to print out the contents of the Exception e? – Dien Feb 17 '20 at 04:13
  • Try [here for various ways](https://stackoverflow.com/q/15722763/1270789). – Ken Y-N Feb 17 '20 at 04:14
  • catch (Exception e) { // printStackTrace method // prints line numbers + call stack e.printStackTrace(); // Prints what exception has been thrown System.out.println(e); } – Dien Feb 17 '20 at 04:24
  • Please copy the text of the error message and [edit] it into your question. – Ken Y-N Feb 17 '20 at 04:31
  • I added two lines in catch section: catch (Exception e) { e.printStackTrace(); System.out.println(e); //System.out.println("something failed"); } – Dien Feb 18 '20 at 03:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208017/discussion-between-dien-and-ken-y-n). – Dien Feb 18 '20 at 03:44

0 Answers0