当前位置: 移动技术网 > IT编程>开发语言>Java > 使用javax.sound实现简单音频播放

使用javax.sound实现简单音频播放

2019年07月19日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了javax.sound实现简单音频播放的具体代码,供大家参考,具体内容如下 /** * @see * @author al_ass

本文实例为大家分享了javax.sound实现简单音频播放的具体代码,供大家参考,具体内容如下


/** 
* @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();  //继续开始播放音频 
     
  } 
 
 } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网