This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Help on stopping method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Help on stopping method..." Watch "Help on stopping method..." New topic
Author

Help on stopping method...

James Dark
Greenhorn

Joined: Mar 06, 2004
Posts: 11
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.");

}
}
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
You can put riminder on a separate thread. Also it looks like you want it to be a singleton.
In the TimerTask, use reference to interrupt the thread (not currentThread()) also in the while loop use volatile boolean run and implement abort() method that will set run=false;

volatile boolean run = true;

public void abort(){
run = false;
}

while(nBytesRead != -1 && run){
...
}


Also, if you are going to make it part of GUI, use KeyEvent and KeyEventListener to trap pressed keys.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help on stopping method...