java - 保存部分音频文件 (Java)

标签 java

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

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

<小时/>
class myThread extends Thread{

    public void run(){
        try {
            PipedOutputStream poStream = new PipedOutputStream();
            PipedInputStream piStream = new PipedInputStream();
            poStream.connect(piStream);
            File cutaudioFile = new File ("file2.wav");

            AudioInputStream ais =
              new AudioInputStream(piStream,
                                   AudioFileFormat.Type.WAVE,
                                   cutaudioFile);
            poStream.write(ais,AudioFileFormat.Type.WAVE,cutaudioFile);
        }catch (Exception e){
            e.printStackTrace();
        }
    } // end run
} // end myThread
<小时/>

最佳答案

这基本上应该是你想要的。它使用关闭 Hook 。

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();
  }
}

关于java - 保存部分音频文件 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2579030/

相关文章:

java - 如何使用 jxl 检查 Excel 文件是否受密码保护

Java SwingUtilities.invokeLater 更新 TextArea

java - 即使其中一个表达式为假,If-else boolean 表达式 (&&) 是否会在 if-else 语句之外递增变量?

java - 为什么我的搜索算法不适用于 Project Euler 31?

java - 我们应该使用 clone 还是 BeanUtils.copyProperties 以及为什么

java - 身份验证后存储附加信息[Spring]

java - Android 3.0系统栏中的菜单

java - 如何使用 Gradle 构建库 Jar

java - JDBC批量插入——控制插入速度

java - 从端点调用时,Spring Boot 获取 UTC+1 而不是 GMT