使用javax.sound实现简单的音频播放
2016-11-18 00:37
585 查看
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年11月17日 下午6:27:59 * @version V1.0 * Description: 简易音频播放器(只支持AU,RA,WAV) * 在不使用JMF的情况下快速实现音频播放 * */ 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(); //继续开始播放音频 } }源码github:https://github.com/Al-assad/Java-Gadgets/tree/master/SimpleMusicPlayer
相关文章推荐
- 使用javax.sound实现简单音频播放
- 25、从头学Android之多媒体--使用SoundPool播放音频
- 使用WindowsAPI实现播放PCM音频的方法
- 一看就会Android之使用SoundPool播放多个短小的音频,如按键提示音,消息提示音,游戏声音等
- 简单谈谈如何利用h5实现音频的播放
- 使用setInterval函数实现简单图片循环播放
- 从头学Android之多媒体--使用SoundPool播放音频
- 最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)
- 最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)
- Android 音频播放之SoundPool的使用和封装
- 简单谈谈如何利用h5实现音频的播放
- 在iphone中使用AudioQueue来实现音频播放功能时最主要的步骤
- 最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)
- 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
- C#实现使用API函数播放MP3等音频文件的类设计
- iOS 简单实现音频的播放
- Android之使用SoundPool播放一小段音频,实现猜歌的功能
- iOS音频播放 (六):简单的音频播放器实现
- 最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)
- 本地音频播放,使用AVFoundation.framework中的AVAudioPlayer来实现