JukeBox, Waveform display



一个播放音频文件的小程序,播放时显示wave form(波形),文件信息(名称,解码,声道,bits,大小)。
暂时只支持.wav文件。

Written by Chengliang_Dong, University of Wollongong. 2006

运行环境: Windows, Mac OS

程序下载: JukeBox

部分代码说明:

LoadMusic method 用于读取指定的wave file并且读取wave file的信息

private void LoadMusic(File f)
    {
         //Init Line to null
        line = null;
        try
        {
            //Get audio inputstream
            au = AudioSystem.getAudioInputStream(f);
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
       
        //get audio format information
        aufm = au.getFormat();
        String c;
       
         //if Channels is one the audio is mono otherwise is stereo
        if (aufm.getChannels() == 1)
        {
            c = "mono";
        }
        else
        {
            c = "stereo";
        }
       
        //read audio information
        Infor = f.getName() + " Encoding:" + aufm.getEncoding() + " SampleRate:" + aufm.getSampleRate() + " " + c
            + " " + aufm.getSampleSizeInBits() + "bits "
            + Math.round(au.getFrameLength()/aufm.getFrameRate()) + "seconds"; //Calculate audio second then round to seconds
       
         //dataline
        DataLine.Info lineInfor = new DataLine.Info(SourceDataLine.class,aufm);
        try
        {
            //assign to line.
            line = (SourceDataLine) AudioSystem.getLine(lineInfor);
        }catch(LineUnavailableException ex)
        {
            ex.printStackTrace();
        }
       
    }

Thread Class AudioT, 用于读取声音缓存,并写如line进行播放。 注意:必须使用thread,以避免读取文件循环过程中程序占用所有资源

//Thread Class to avoid program using all the resources
    class AudioT extends Thread{
        //init nBytesRead to store how many data will read
        int nBytesRead =1;
         //create a buffData array to store the audio information
        byte buffData []= new byte[buffSize];
       
        public void run(){
            try{
                //if nBytesRead not equa -1, which means there is an audio file waiting to play
                while (nBytesRead!=-1)
                {
                    //if play flag is true
                    if (play_flag == true)
                    {
                         //check if the line lis running, if not, open the line and start it
                        if (!line.isRunning())
                        {
                            line.open(aufm);
                            line.start();
                           
                        }
                    try{
                            //read the 256 bytes information
                            nBytesRead = au.read(buffData,0,buffData.length);
                           
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                    if (nBytesRead >=0){
                       
                          //write to line, so the program will play the information  (music)
                        int nBytesWritten = line.write(buffData,0,nBytesRead);
                       
                        //getData method, to generate a 128 bytes audio waveform information
                        short[] xPoints = getData(buffData,aufm.getChannels());
                         //pass the data to Canvas Panel to display the waveform
                        cv.getData(xPoints);
                    }
                    }
                } //end of while loop, which means that the file is ended
               
                //clear the buffer and send the empty information to Canvas so it display a line
                short[] xPoints;
                xPoints = new short[128];
                for (int i = 0; i < xPoints.length; i++)
                {
                    xPoints[i] = 0;
                }
                cv.getData(xPoints);
               
                //stop and close the line
                line.drain();
                line.stop();
                line.close();
                line = null;
                play_flag = false;
                 //if the repeat tag is true. reopen the file and play it
                if (repeat)
                {
                    PlayAudio();
                }
           
            }catch(Exception e){
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值