2010-04-05 5 views
1

Pendant la lecture d'un fichier audio (.wav) Je veux, si je recours à Ctrl +C, pour arrêter la lecture et enregistrer une partie du fichier audio dans un fichier appelé "fichier2.wav".sauver une partie d'un fichier audio (Java)

Voici le fil que j'aimerais ajouter à mon code. Malheureusement, cela ne fonctionne pas du tout.


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

Vous essayez d'enregistrer la partie non jouée du fichier wav sur le disque lorsque vous appuyez sur Ctrl-C? –

+0

Votre nouvelle partie AudioInputStream (...) 'est-elle correcte? Constructeur affiché à l'adresse http://java.sun.com/javase/6/docs/api/javax/sound/sampled/AudioInputStream.html#AudioInputStream(java.io.InputStream,%20javax.sound.sampled.AudioFormat,%20long) prend un 'long' comme troisième paramètre et non un' fichier' – barrowc

Répondre

0

Cela devrait être essentiellement ce que vous voulez. Il utilise un crochet d'arrêt.

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