保存部分音频文件(Java)

保存部分音频文件(Java),java,Java,播放音频文件(.wav)时,如果我使用Ctrl+C,我希望停止播放并将部分音频文件保存在名为“file2.wav”的文件中 下面是我想添加到代码中的线程。 不幸的是,它根本不起作用 这基本上就是你想要的。它使用一个关机挂钩 import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.

播放音频文件(.wav)时,如果我使用Ctrl+C,我希望停止播放并将部分音频文件保存在名为“file2.wav”的文件中

下面是我想添加到代码中的线程。 不幸的是,它根本不起作用



这基本上就是你想要的。它使用一个关机挂钩

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Clip;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CtrlCAudio
{
  public static void main(String[] args) throws LineUnavailableException, UnsupportedAudioFileException, IOException
  {
    final File inputAudio = new File(args[0]);
    final File outputAudio = new File(args[1]);
    // 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.
    final Clip 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();
  }
}

点击Ctrl-C时,您正试图将wav文件的未显示部分保存到磁盘?您的
新音频输入流(…)
部分是否正确?在中显示的构造函数将
long
作为第三个参数,而不是
文件
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Clip;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CtrlCAudio
{
  public static void main(String[] args) throws LineUnavailableException, UnsupportedAudioFileException, IOException
  {
    final File inputAudio = new File(args[0]);
    final File outputAudio = new File(args[1]);
    // 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.
    final Clip 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();
  }
}