2017-05-02 43 views
0

當我按下按鈕時,netbeans本身說:「線程中的異常」AWT-EventQueue-0「java.lang.IllegalArgumentException:無行匹配接口TargetDataLine支持格式PCM_SIGNED 44100.0 Hz,16位,單聲道,2字節/幀,支持big-endian。「 當該行不受支持時,應該彈出一條錯誤消息,提示「行不受支持」。相反,沒有任何反應。 我該怎麼辦?正在執行消息對話框

public class Ouvir extends NewJFrame{ 

AudioFormat audioFormat; 
TargetDataLine targetDataLine; 
TargetDataLine line; 

void captureAudio(){ 

      Listen.setEnabled(false); 
      try{ 
      audioFormat = getAudioFormat(); 
      DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); 
      line = (TargetDataLine) AudioSystem.getLine(info); 
      AudioSystem.getLine(info); 

      if (!AudioSystem.isLineSupported(info)) { 
       String error = "Line not supported"; 
       JOptionPane.showMessageDialog(null,error,"+",JOptionPane.ERROR_MESSAGE); 
       line.close(); 
      } 

      line.open(); 
      line.start(); 
     } 
      catch (LineUnavailableException e) {} 
     } 

void stopCapture(){ 

    if(line != null) 
     { 
     line.stop(); 
     line.close(); 
     } 
    if(!Stop.getModel().isPressed()) 
     { 
     line.stop(); 
     line.close(); 
     } 
     } 

private AudioFormat getAudioFormat(){ 


     return new AudioFormat(44100,16,1,true,true); 
    } 
    } 
+0

你在測試之前得到AudioLine是否支持 – MadProgrammer

+0

我該如何解決這個問題?沒有完全獲得 –

+0

您需要在AudioSystem.getLine(info)之前調用'AudioSystem.isLineSupported(info)',否則,你怎麼知道它是否可以被支持 – MadProgrammer

回答

1

你在做什麼基本上是試圖檢查,看看是否有可能

AudioSystem.getLine(info); 
if (!AudioSystem.isLineSupported(info)) {... 

getLine拋出不受支持的例外,因爲你先叫之前得到AudioLine。你需要扭轉你的邏輯

if (AudioSystem.isLineSupported(info)) { 
    AudioSystem.getLine(info); 
} else { 
    // Show error 
}