使用javax.sound.sampled.Clip播放,循環(huán)播放和停止游戲中的多種聲音。意外錯誤
我能夠使代碼正常工作,現(xiàn)在對Clips有了更好的了解。該頁面對我?guī)椭畲蟮捻撁媸莌ttp://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html,它分解了所有內(nèi)容,并幫助我查看了哪里出錯了。這是我的最終工作代碼。和以前一樣,如果您看到任何可怕的錯誤或邏輯或樣式方面的問題,請告訴我。
import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.UnsupportedAudioFileException;/** * Handles playing, stoping, and looping of sounds for the game. * @author Tyler Thomas * */public class Sound { private Clip clip; public Sound(String fileName) {// specify the sound to play// (assuming the sound can be played by the audio system)// from a wave Filetry { File file = new File(fileName); if (file.exists()) {AudioInputStream sound = AudioSystem.getAudioInputStream(file); // load the sound into memory (a Clip)clip = AudioSystem.getClip();clip.open(sound); } else {throw new RuntimeException('Sound: file not found: ' + fileName); }}catch (MalformedURLException e) { e.printstacktrace(); throw new RuntimeException('Sound: Malformed URL: ' + e);}catch (UnsupportedAudioFileException e) { e.printstacktrace(); throw new RuntimeException('Sound: Unsupported Audio File: ' + e);}catch (IOException e) { e.printstacktrace(); throw new RuntimeException('Sound: Input/Output Error: ' + e);}catch (LineUnavailableException e) { e.printstacktrace(); throw new RuntimeException('Sound: Line Unavailable Exception Error: ' + e);} // play, stop, loop the sound clip } public void play(){clip.setFramePosition(0); // Must always rewind!clip.start(); } public void loop(){clip.loop(Clip.LOOP_CONTINUOUSLY); } public void stop(){ clip.stop();} }解決方法
我正在嘗試在游戲中一次播放兩個波形聲音(背景音樂和效果)。我首先使用Java中的另一個音頻處理程序構(gòu)造了這段代碼,該處理程序?qū)⑻幚砺曇舻牟シ牛V购脱h(huán)。這種構(gòu)造只會播放背景音樂或效果,但一次只能播放一次。我環(huán)顧互聯(lián)網(wǎng),并被告知使用javax.sound.sampled.Clip處理聲音,因此重用了相同的構(gòu)造(播放,停止,循環(huán)),但將其切換為使用javax.sound.sampled.Clip。現(xiàn)在我完全迷路了。從到目前為止的內(nèi)容來看,我已經(jīng)完成了所有正確的操作,并且在eclipse編輯器中沒有出現(xiàn)任何錯誤,但是當(dāng)我運行它時,我遇到了兩個錯誤之一。在Eclipse中(在Linux上運行),拋出LineUnavailableException。在Eclipse中(在Windows7上運行),我在此代碼的loop()部分中獲得了java.lang.NullPointerException。如果您可以告訴我我做錯了什么,或者向我指出一些相關(guān)的文檔,我將不勝感激。我假設(shè)我的代碼可以處理異常,但是我不確定。如果您發(fā)現(xiàn)其他任何令人毛骨悚然的代碼失誤,請讓我知道我正在努力成為最好的程序員,并真的感謝建設(shè)性的批評。感謝您的時間。
import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; /** * Handles play,pause,and looping of sounds for the game. * @author Tyler Thomas * */ public class Sound {private Clip myClip;public Sound(String fileName) {try { File file = new File(fileName); if (file.exists()) {Clip myClip = AudioSystem.getClip();AudioInputStream ais = AudioSystem.getAudioInputStream(file.toURI().toURL());myClip.open(ais); } else {throw new RuntimeException('Sound: file not found: ' + fileName); }}catch (MalformedURLException e) { throw new RuntimeException('Sound: Malformed URL: ' + e);}catch (UnsupportedAudioFileException e) { throw new RuntimeException('Sound: Unsupported Audio File: ' + e);}catch (IOException e) { throw new RuntimeException('Sound: Input/Output Error: ' + e);}catch (LineUnavailableException e) { throw new RuntimeException('Sound: Line Unavailable: ' + e);}}public void play(){ myClip.setFramePosition(0); // Must always rewind! myClip.loop(0); myClip.start();}public void loop(){ myClip.loop(Clip.LOOP_CONTINUOUSLY);}public void stop(){ myClip.stop();} }
