Hi. The code below is to play a song but needs to stop when the user types the number "1." How do you make it so the public Reminder(int seconds) will stop which will then make the song stop? After it stops, I want to be able to do other things so I don't want the program to end.
Thanks for any help.
import apcslib.*;
import chn.util.*;
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.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.util.Timer;
import java.util.TimerTask;
public class Reminder {
Timer timer;
private static final intEXTERNAL_BUFFER_SIZE = 128000;
ConsoleIO console = new ConsoleIO();
public Reminder(int seconds) {
timer = new Timer(true);
timer.schedule(new RemindTask(), seconds*1000);
File soundFile = new File("C:\\Program Files\\Counter-Strike\\cstrike\\sound\\training\\cstrain6.wav");
AudioInputStreamaudioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
AudioFormataudioFormat = audioInputStream.getFormat();
SourceDataLineline = null;
DataLine.Infoinfo = new DataLine.Info(SourceDataLine.class, audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
line.start();
intnBytesRead = 0;
byte[]abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
intnBytesWritten = line.write(abData, 0, nBytesRead);
}
}
line.drain();
line.close();
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer
thread int a;
ConsoleIO console = new ConsoleIO();
System.out.print("Would you choose 1 or 2: ");
a = console.readInt();
if (a == 1)
Thread.currentThread().interrupt();//Need help here...
}
}
public static void main(
String args[]) {
System.out.println("About to schedule task.");
new Reminder(0);
System.out.println("Task scheduled.");
}
}