• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help on stopping method...

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.");

}
}
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic