2010-04-05 52 views
1

在播放音频文件(.WAV)我想,如果我再打按Ctrl +Ç,停止播放和保存音频文件的一部分文件称为“file2.wav”。部分(JAVA)

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


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 

+0

您试图在按Ctrl-C时将wav文件的未播放部分保存到磁盘? – 2010-04-05 15:06:47

+0

你的'新的AudioInputStream(...)'部分是否正确?显示在http://java.sun.com/javase/6/docs/api/javax/sound/sampled/AudioInputStream.html#AudioInputStream(javajava.io.InputStream,%20javax.sound.sampled.AudioFormat,%20long )采用'long'作为第三个参数,而不是'File' – barrowc 2010-04-07 02:22:50

回答

0

这应该是基本上你想要什么。它使用关闭挂钩。

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