使用javax.sound實(shí)現(xiàn)簡(jiǎn)單音頻播放
更新時(shí)間:2018年03月31日 11:00:16 作者:Al_assad
這篇文章主要為大家詳細(xì)介紹了使用javax.sound實(shí)現(xiàn)簡(jiǎn)單音頻播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了javax.sound實(shí)現(xiàn)簡(jiǎn)單音頻播放的具體代碼,供大家參考,具體內(nèi)容如下
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年11月17日 下午6:27:59 * @version V1.0 * Description: 簡(jiǎn)易音頻播放器(只支持AU,RA,WAV) * 在不使用JMF的情況下快速實(shí)現(xiàn)音頻播放 * */ import javax.sound.sampled.*; import java.io.*; public class MusicPlayer { private String musicPath; //音頻文件 private volatile boolean run = true; //記錄音頻是否播放 private Thread mainThread; //播放音頻的任務(wù)線程 private AudioInputStream audioStream; private AudioFormat audioFormat; private SourceDataLine sourceDataLine; public MusicPlayer(String musicPath) { this.musicPath = musicPath; prefetch(); } //數(shù)據(jù)準(zhǔn)備 private void prefetch(){ try{ //獲取音頻輸入流 audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); //獲取音頻的編碼對(duì)象 audioFormat = audioStream.getFormat(); //包裝音頻信息 DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED); //使用包裝音頻信息后的Info類創(chuàng)建源數(shù)據(jù)行,充當(dāng)混頻器的源 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(); } } //析構(gòu)函數(shù):關(guān)閉音頻讀取流和數(shù)據(jù)行 protected void finalize() throws Throwable{ super.finalize(); sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } //播放音頻:通過loop參數(shù)設(shè)置是否循環(huán)播放 private void playMusic(boolean loop)throws InterruptedException { try{ if(loop){ while(true){ playMusic(); } }else{ playMusic(); //清空數(shù)據(jù)行并關(guān)閉 sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } }catch(IOException ex){ ex.printStackTrace(); } } private void playMusic(){ try{ synchronized(this){ run = true; } //通過數(shù)據(jù)行讀取音頻數(shù)據(jù)流,發(fā)送到混音器; //數(shù)據(jù)流傳輸過程: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(); } } //繼續(xù)播放音樂 private void continueMusic(){ synchronized(this){ run = true; notifyAll(); } } //外部調(diào)用控制方法:生成音頻主線程; public void start(boolean loop){ mainThread = new Thread(new Runnable(){ public void run(){ try { playMusic(loop); } catch (InterruptedException e) { e.printStackTrace(); } } }); mainThread.start(); } //外部調(diào)用控制方法:暫停音頻線程 public void stop(){ new Thread(new Runnable(){ public void run(){ stopMusic(); } }).start(); } //外部調(diào)用控制方法:繼續(xù)音頻線程 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"); //創(chuàng)建音樂播放器 player.start(true); //以開始以循環(huán)的形式播放,player(false)為不循環(huán)播放 TimeUnit.SECONDS.sleep(5); player.stop(); //暫停播放音頻 TimeUnit.SECONDS.sleep(4); player.continues(); //繼續(xù)開始播放音頻 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java?List中五種常見實(shí)現(xiàn)類的使用
Java中提供了非常多的使用的List實(shí)現(xiàn)類,本文將重點(diǎn)介紹一下常見的五種實(shí)現(xiàn)類以及他們的應(yīng)用場(chǎng)景,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10Sentinel網(wǎng)關(guān)限流與SpringCloud Gateway整合過程
本文介紹了如何通過SpringCloudGateway集成阿里的Sentinel進(jìn)行網(wǎng)關(guān)限流,Sentinel作為流量防衛(wèi)兵,提供了豐富的應(yīng)用場(chǎng)景和完備的實(shí)時(shí)監(jiān)控功能,通過配置路由維度和自定義API維度的限流規(guī)則,實(shí)現(xiàn)了對(duì)微服務(wù)的保護(hù)2024-11-11