2016-04-10 23 views
1

Ich bin ein neues Audio-System für mein Spiel zu schreiben, und ich habe über diesen Fehler kam und kann nicht überall zu finden und Lösung scheint,Fehler beim Laden der Audio-in Java (illegal call() in der Schnittstelle Clip zu öffnen)

java.lang.IllegalArgumentException: illegal call to open() in interface Clip 
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source) 
at com.sun.media.sound.AbstractDataLine.open(Unknown Source) 
at com.sun.media.sound.AbstractDataLine.open(Unknown Source) 

Dies ist der Code, den ich verwende, um meine Audiodaten zu laden und abzuspielen.

private Clip load(String filename) {   
    try { 
     //Loads the file 
     InputStream in = new FileInputStream(new File("res/" + filename + FILE_EXT)); 
     //Create the input buffer 
     InputStream bufferedIn = new BufferedInputStream(in); 
     //Convert into an audio stream 
     AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn); 
     //Get the audio format 
     AudioFormat format = audioStream.getFormat(); 
     //Get the data line info 
     DataLine.Info info = new DataLine.Info(Clip.class, format); 
     //Return the clip 
     Clip audioClip = (Clip) AudioSystem.getLine(info); 
     audioClip.addLineListener(this); 
     return this.clip = audioClip; 
    } catch (FileNotFoundException e) { 
     System.err.println("Failed to load audio! " + filename + " not found!"); 
     throw new RuntimeException(e); 
    } catch (UnsupportedAudioFileException e) { 
     System.err.println("Failed to load audio! " + filename + " is unsupported!"); 
     throw new RuntimeException(e); 
    } catch (IOException e) { 
     System.err.println("Failed to load audio! " + filename + " caused an IO Exception!"); 
     throw new RuntimeException(e); 
    } catch (LineUnavailableException e) { 
     System.err.println("Failed to load audio! " + filename + " line is unavalible!"); 
     e.printStackTrace(); 
    } 
    throw new RuntimeException("Failed to load audio! input == null!"); 
} 

private void startClip() { 
    if(this.clip != null) this.clip.start(); 
    else throw new RuntimeException("Failed to start audio clip! The clip appears to be null."); 
} 

private void stopClip() { 
    if(this.clip != null) this.clip.close(); 
    else throw new RuntimeException("Failed to close audio clip! The clip appears to be null."); 
} 

@Override 
public void play() { 
    try { 
     if(isPlaying()) return; 
     else { 
      startClip(); 
      this.clip.open(); 
      this.playing = true; 
     } 
    } catch (LineUnavailableException e) { 
     e.printStackTrace(); 
    } 
} 

Der Fehler tritt bei this.clip.open();

Kann mir jemand helfen?

Antwort

2

Sie geben nichts an die Clip zu spielen.

Line#open:

IllegalArgumentException - wenn diese Methode auf einer Clip Instanz aufgerufen wird.

Sie müssen clip.open(audioStream) statt clip.open() anrufen. Auch müssen Sie dies vor beginnend die Clip tun.