Reputation: 2626
I am designing a simple timer application, but I've got some troubles with playing sound.
Here is my code
public class Timer {
private static int time = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Type the values");
System.out.print("Hours : ");
int hours = scanner.nextInt();
System.out.print("Minutes : ");
int minutes = scanner.nextInt();
System.out.print("Seconds : ");
int seconds = scanner.nextInt();
time = hours * 3600 + minutes * 60 + seconds;
new Thread() {
@Override
public void run() {
try {
while (time != 0) {
time--;
sleep(1000);
}
System.out.println("Time elapsed");
URL url = Timer.class.getResource("Timer.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (InterruptedException | UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
}.start();
System.out.println("Timer started");
}
}
And here is my project structure. Now the problem is that application does not give me any exception, though it does not play the sound. What's wrong?
Upvotes: 3
Views: 1993
Reputation: 2626
The problem was that the application exited before the clip even started to play. So I've replaced my playing block with this new one.
URL url = Timer.class.getResource("Timer.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
AudioFormat format = audioIn.getFormat();
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
long frames = audioIn.getFrameLength();
double durationInSeconds = (frames + 0.0) / format.getFrameRate();
sleep((long) durationInSeconds * 1000);
Now it waits until the clip plays and finishes and only then the application finishes it's job
Upvotes: 1
Reputation: 5533
This is actually a multi-threading issue.
The problem is that you start the clip, but then terminate your program without giving it a chance to play to it's end. The Clip.start()
method is not a blocking operation, which means it does not wait, but rather starts a new daemon thread to play the sound, a daemon thread which is killed once the program exits the main
method.
Here is a code example from my other answer, for playing an audio file using a Clip
. Notice the way I calculate the sound duration and then sleep()
to let it play.
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class PlaySound {
private static boolean tryToInterruptSound = false;
private static long mainTimeOut = 3000;
private static long startTime = System.currentTimeMillis();
public static synchronized Thread playSound(final File file) {
Thread soundThread = new Thread() {
@Override
public void run() {
try{
Clip clip = null;
AudioInputStream inputStream = null;
clip = AudioSystem.getClip();
inputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = inputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
long durationInMiliSeconds =
(long) (((float)audioFileLength / (frameSize * frameRate)) * 1000);
clip.open(inputStream);
clip.start();
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound started playing!");
Thread.sleep(durationInMiliSeconds);
while (true) {
if (!clip.isActive()) {
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound got to it's end!");
break;
}
long fPos = (long)(clip.getMicrosecondPosition() / 1000);
long left = durationInMiliSeconds - fPos;
System.out.println("" + (System.currentTimeMillis() - startTime) + ": time left: " + left);
if (left > 0) Thread.sleep(left);
}
clip.stop();
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound stoped");
clip.close();
inputStream.close();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound interrupted while playing.");
}
}
};
soundThread.setDaemon(true);
soundThread.start();
return soundThread;
}
public static void main(String[] args) {
Thread soundThread = playSound(new File("C:\\Booboo.wav"));
System.out.println("" + (System.currentTimeMillis() - startTime) + ": playSound returned, keep running the code");
try {
Thread.sleep(mainTimeOut );
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tryToInterruptSound) {
try {
soundThread.interrupt();
Thread.sleep(1);
// Sleep in order to let the interruption handling end before
// exiting the program (else the interruption could be handled
// after the main thread ends!).
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("" + (System.currentTimeMillis() - startTime) + ": End of main thread; exiting program " +
(soundThread.isAlive() ? "killing the sound deamon thread" : ""));
}
}
Upvotes: 4
Reputation: 10136
You should test to see if the resource was actually retrieved successfully:
URL url = Timer.class.getResource("Timer.wav");
if (url == null)
{
System.out.println("wav file resource not found!");
return;
}
// continue on
Also add an uncaught exception handler to your program to see if any exceptions are escaping all the way to the top of your program:
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
String msg = "DefaultUncaughtExceptionHandler thread[" + t.getName() + "]";
System.out.println(msg);
e.printStackTrace(System.out);
System.err.println(msg);
e.printStackTrace(System.err);
}
});
Upvotes: 0
Reputation: 616
Edit: It may help if I read the whole code before posting an answer lol. Ok so your class.getResource("Timer.wav") should actually be class.getResource("resources/Timer.wav") I'm assuming resources is a folder in your class structure. Also if you use getResourceAsStream instead of getResource you can skip the URL thing.
Upvotes: 0