2011-01-01 7 views
-2

Ich habe ein Programm für Audio-Aufnahme gemacht ... Überprüfen Sie diese .Während der Audioaufnahme ist eine Ausnahme aufgetreten ...!

import java.io.*; 
import javax.sound.sampled.*; 
public class pp extends Thread 
{ 
    TargetDataLine tdl; 
    AudioFileFormat.Type afft; 
    AudioInputStream ais; 
    File f1; 
    public pp(TargetDataLine l,AudioFileFormat.Type t,File f2) 
    { 
     tdl=l; 
     ais=new AudioInputStream(tdl); 
     t=afft; 
     f1=f2; 
    } 
    public void start() 
    { 
     tdl.start(); 
     super.start(); 
    } 
    public void sr() 
    { 
     tdl.stop(); 
     tdl.close(); 
    } 
    public void run() 
    { 
     try 
     { 
      AudioSystem.write(ais,afft,f1); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      System.out.println("Exception caught "); 
     } 
    } 
     public static void main(String args[]) 
     { 
      String s=args[0]; 
      File f=new File(s); 
      AudioFormat ff=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100.0F, 16, 2, 4, 44100.0F, false); 
      DataLine.Info inf=new DataLine.Info(TargetDataLine.class,ff); 
      TargetDataLine td=null; 
      try 
      { 
       td=(TargetDataLine)AudioSystem.getLine(inf); 
       td.open(ff); 
      } 
      catch(Exception e) 
      { 
       //e.printStackTrace(); 
       System.out.println("Exception caught "); 
      } 
      AudioFileFormat.Type t5=AudioFileFormat.Type.AIFC; 
      pp p=new pp(td,t5,f); 
      System.out.println("enter to start recording"); 
      try 
      { 
       System.in.read(); 
      } 
      catch(Exception e) 
      { 
       System.out.println("exception caught "); 
      } 
      p.start(); 
      System.out.println("enter to stop recording"); 
      try 
      { 
       System.in.read(); 
      } 
      catch(Exception e) 
      { 
       System.out.println("exception caught "); 
      } 
      p.sr(); 
      System.out.println("recording stoped"); 
     } 
} 

eine Ausnahme zeigen wie ....

java.lang.IllegalArgumentException: could not write audio file: file type not supported: null 
    at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1363) 
    at pp.run(pp.java:30) 

Kann jemand mir helfen, dieses Problem bei der Festsetzung .... !!

+25

Hinweis für die allgemeine Codierung: machen Sie Ihren Namen sinnvoll * *. Der Versuch, Code wie diesen zu verstehen, ist wie Zähne ziehen. –

Antwort

30

Hier ist dein Fehler:

public pp(TargetDataLine l,AudioFileFormat.Type t,File f2) 
{ 
    tdl=l; 
    ais=new AudioInputStream(tdl); 
    // This line doesn't do anything useful, does it? 
    t=afft; 
    f1=f2; 
} 

Statt

t = afft; 

die den Parameter Wert auf die Variable Instanz Wert setzt, wollen Sie es andersrum:

afft = t; 

Von Natürlich könnte man klarere Namen verwenden, und verwenden die gleichen Namen für die Parameter und Instanzvariablen, dann this verwenden, um festzulegen, welche zu verwenden:

public AudioRecorder(TargetDataLine inputLine, 
        AudioFileFormat.Type fileType 
        File outputFile) 
{ 
    this.inputLine = inputLine; 
    this.inputStream = new AudioInputStream(inputLine); 
    this.fileType = fileType; 
    this.outputFile = outputFile; 
} 
+5

Ich bewundere deine Geduld. Ja wirklich. Als ich zu "t = afft" kam, konnte ich mich nicht erinnern, was ich sah oder wo ich war. – Nivas

+2

Wow, Jon. Sie sollten einen Entschlüsselungsjob in MI6 bekommen ... – configurator

+0

Sehr clever Jon – Nicholas

Verwandte Themen