您好,登錄后才能下訂單哦!
使用javax.sound怎么實現音頻播放功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
具體內容如下
import javax.sound.sampled.*; import java.io.*; public class MusicPlayer { private String musicPath; //音頻文件 private volatile boolean run = true; //記錄音頻是否播放 private Thread mainThread; //播放音頻的任務線程 private AudioInputStream audioStream; private AudioFormat audioFormat; private SourceDataLine sourceDataLine; public MusicPlayer(String musicPath) { this.musicPath = musicPath; prefetch(); } //數據準備 private void prefetch(){ try{ //獲取音頻輸入流 audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); //獲取音頻的編碼對象 audioFormat = audioStream.getFormat(); //包裝音頻信息 DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED); //使用包裝音頻信息后的Info類創建源數據行,充當混頻器的源 sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); }catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(LineUnavailableException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); } } //析構函數:關閉音頻讀取流和數據行 protected void finalize() throws Throwable{ super.finalize(); sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } //播放音頻:通過loop參數設置是否循環播放 private void playMusic(boolean loop)throws InterruptedException { try{ if(loop){ while(true){ playMusic(); } }else{ playMusic(); //清空數據行并關閉 sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } }catch(IOException ex){ ex.printStackTrace(); } } private void playMusic(){ try{ synchronized(this){ run = true; } //通過數據行讀取音頻數據流,發送到混音器; //數據流傳輸過程:AudioInputStream -> SourceDataLine; audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); int count; byte tempBuff[] = new byte[1024]; while((count = audioStream.read(tempBuff,0,tempBuff.length)) != -1){ synchronized(this){ while(!run) wait(); } sourceDataLine.write(tempBuff,0,count); } }catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }catch(InterruptedException ex){ ex.printStackTrace(); } } //暫停播放音頻 private void stopMusic(){ synchronized(this){ run = false; notifyAll(); } } //繼續播放音樂 private void continueMusic(){ synchronized(this){ run = true; notifyAll(); } } //外部調用控制方法:生成音頻主線程; public void start(boolean loop){ mainThread = new Thread(new Runnable(){ public void run(){ try { playMusic(loop); } catch (InterruptedException e) { e.printStackTrace(); } } }); mainThread.start(); } //外部調用控制方法:暫停音頻線程 public void stop(){ new Thread(new Runnable(){ public void run(){ stopMusic(); } }).start(); } //外部調用控制方法:繼續音頻線程 public void continues(){ new Thread(new Runnable(){ public void run(){ continueMusic(); } }).start(); } //Test public static void main(String[] args) throws InterruptedException{ MusicPlayer player = new MusicPlayer("bgm/1.wav"); //創建音樂播放器 player.start(true); //以開始以循環的形式播放,player(false)為不循環播放 TimeUnit.SECONDS.sleep(5); player.stop(); //暫停播放音頻 TimeUnit.SECONDS.sleep(4); player.continues(); //繼續開始播放音頻 } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。