//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