Avoiding shutdown hook

Posted by meryl on Stack Overflow See other posts from Stack Overflow or by meryl
Published on 2010-04-24T14:33:33Z Indexed on 2010/04/24 14:43 UTC
Read the original article Hit count: 363

Filed under:

Through the following code I can play and cut and audio file. Is there any other way to avoid using a shutdown hook? The problem is that whenever I push the cut button , the file doesn't get saved until I close the application

thanks

......................

void play_cut() {

    try {



// First, we get the format of the input file
final AudioFileFormat.Type fileType = AudioSystem.getAudioFileFormat(inputAudio).getType();
// Then, we get a clip for playing the audio.
c = AudioSystem.getClip();
// We get a stream for playing the input file.
AudioInputStream ais = AudioSystem.getAudioInputStream(inputAudio);
// We use the clip to open (but not start) the input stream
c.open(ais);
// We get the format of the audio codec (not the file format we got above)
final AudioFormat audioFormat = ais.getFormat();

 // We add a shutdown hook, an anonymous inner class.
Runtime.getRuntime().addShutdownHook(new Thread()
{
  public void run()
  {
    // We're now in the hook, which means the program is shutting down.
    // You would need to use better exception handling in a production application.
    try
    {
      // Stop the audio clip.
      c.stop();
      // Create a new input stream, with the duration set to the frame count we reached.  Note that we use the previously determined audio format
      AudioInputStream startStream = new AudioInputStream(new FileInputStream(inputAudio), audioFormat, c.getLongFramePosition());
      // Write it out to the output file, using the same file type.
      AudioSystem.write(startStream, fileType, outputAudio);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
  }
});
// After setting up the hook, we start the clip.


 c.start();

    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}// end play_cut

......................

© Stack Overflow or respective owner

Related posts about java