Java 避免停机挂钩

Java 避免停机挂钩,java,Java,通过以下代码,我可以播放和剪切音频文件。 有没有其他方法可以避免使用关机挂钩? 问题是,每当我按下“剪切”按钮时,文件都不会被保存,直到我关闭应用程序 谢谢 void play_cut() { try { // First, we get the format of the input file final AudioFileFormat.Type fileType = AudioSystem.getAudioFileFormat(inputAudio).ge

通过以下代码,我可以播放和剪切音频文件。 有没有其他方法可以避免使用关机挂钩? 问题是,每当我按下“剪切”按钮时,文件都不会被保存,直到我关闭应用程序

谢谢

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

实际上,我想知道的是: 我真的需要关机挂钩吗

如果我移动这两个代码语句

AudioInputStream startStream = new AudioInputStream(new FileInputStream(inputAudio), audioFormat, c.getLongFramePosition());
AudioSystem.write(startStream, fileType, outputAudio);
c.start()之后的其他地方;我得到一个错误:

异常java.io.IOException从不在相应的try语句-->catch(IOException e)的主体中抛出


你认为我不必使用钩子就能得到同样的结果吗?

首先,你所有的评论都是多余的,你只需重复你正在使用的各种类和方法的名称


至于问题,您的保存代码在shutdown钩子中,这意味着“在应用程序即将关闭时执行此操作”,这自然意味着它在程序即将关闭之前不会保存它。所以,把这个逻辑从关闭钩子移到它的逻辑位置——最有可能在方法的末尾,可能在
final
block-even中——就是这样。

首先,你所有的注释都是完全多余的,你只需重复你正在使用的各种类和方法的名称


至于问题,您的保存代码在shutdown钩子中,这意味着“在应用程序即将关闭时执行此操作”,这自然意味着它在程序即将关闭之前不会保存它。所以,把这个逻辑从关闭钩子移到它的逻辑位置——很可能在方法的末尾,可能在
final
block-even中——就是这样。

你不能随时调用你在关闭钩子中已经使用的方法吗?你在这里提到的错误是一个编译器错误,警告您捕获的异常从未在相应的try块中抛出。调用
AudioSystem.write
会引发IOException,当您将调用移到try块之外时,该try块无法引发异常。您必须移动完整的try-catch块,而不是仅移动这两条语句。您不能随时调用关机挂钩中已经使用的方法吗?您在这里提到的错误是一个编译器错误,警告您捕获的异常从未在相应的try块中抛出。调用
AudioSystem.write
会引发IOException,当您将调用移到try块之外时,该try块无法引发异常。您必须移动完整的try-catch块,而不是仅移动这两条语句。