<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    自由飛翔

    我在仰望,java之上

    統計

    留言簿(2)

    我關注的blog

    閱讀排行榜

    評論排行榜

    java播放wav的基礎代碼(轉載)

     文章來源:http://hi.baidu.com/breezedancer/blog/item/7eebb499680d8f086e068cb9.html 

    import javax.sound.sampled.*;
    import java.io.*;
    public class TestMusic{
     
     private AudioFormat format;
        private byte[] samples;
     
     public static void main(String args[])throws Exception{
      TestMusic sound =new TestMusic("1.wav");
      InputStream stream =new ByteArrayInputStream(sound.getSamples());
            // play the sound
            sound.play(stream);
            // exit
            System.exit(0);
     }
     
        public TestMusic(String filename) {
            try {
                // open the audio input stream
                AudioInputStream stream =AudioSystem.getAudioInputStream(new File(filename));
                format = stream.getFormat();
                // get the audio samples
                samples = getSamples(stream);
            }
            catch (UnsupportedAudioFileException ex) {
                ex.printStackTrace();
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
       }
       
       public byte[] getSamples() {
            return samples;
        }
       
         private byte[] getSamples(AudioInputStream audioStream) {
            // get the number of bytes to read
            int length = (int)(audioStream.getFrameLength() * format.getFrameSize());

            // read the entire stream
            byte[] samples = new byte[length];
            DataInputStream is = new DataInputStream(audioStream);
            try {
                is.readFully(samples);
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }

            // return the samples
            return samples;
        }
     
     public void play(InputStream source) {

            // use a short, 100ms (1/10th sec) buffer for real-time
            // change to the sound stream
            int bufferSize = format.getFrameSize() *
                Math.round(format.getSampleRate() / 10);
            byte[] buffer = new byte[bufferSize];

            // create a line to play to
            SourceDataLine line;
            try {
                DataLine.Info info =
                    new DataLine.Info(SourceDataLine.class, format);
                line = (SourceDataLine)AudioSystem.getLine(info);
                line.open(format, bufferSize);
            }
            catch (LineUnavailableException ex) {
                ex.printStackTrace();
                return;
            }

            // start the line
            line.start();

            // copy data to the line
            try {
                int numBytesRead = 0;
                while (numBytesRead != -1) {
                    numBytesRead =
                        source.read(buffer, 0, buffer.length);
                    if (numBytesRead != -1) {
                       line.write(buffer, 0, numBytesRead);
                    }
                }
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }

            // wait until all data is played, then close the line
            line.drain();
            line.close();

        }


    }



    Gavin

    posted on 2012-03-14 14:30 GavinMiao 閱讀(1472) 評論(0)  編輯  收藏 所屬分類: corejava

    主站蜘蛛池模板: 四虎国产精品免费久久| 在线免费观看你懂的| 免费一级一片一毛片| 亚洲精品国产首次亮相| 免费黄色网址入口| 亚洲av成人一区二区三区在线播放| 在线免费视频一区二区| 亚洲精品无码高潮喷水A片软| 精品久久洲久久久久护士免费 | 国产性爱在线观看亚洲黄色一级片| 黄色毛片免费网站| 国产亚洲精品免费视频播放| 中国毛片免费观看| 色播亚洲视频在线观看| 亚洲三级高清免费| 亚洲国产日韩精品| 哒哒哒免费视频观看在线www| 日韩电影免费在线观看网址| 亚洲中文字幕无码一区二区三区| 国产啪精品视频网站免费尤物| 精品亚洲成AV人在线观看| 18未年禁止免费观看| 亚洲精品一卡2卡3卡四卡乱码| 免费播放春色aⅴ视频| 麻豆精品不卡国产免费看| 亚洲第一区视频在线观看| 成人人免费夜夜视频观看| 无遮挡免费一区二区三区| 亚洲国产精品无码专区影院| 久久www免费人成看片| 欧洲亚洲国产精华液| 亚洲AV美女一区二区三区| 女人18毛片水真多免费播放| 国产精品九九久久免费视频| 亚洲视频一区在线观看| 日韩免费高清一级毛片在线| 巨胸狂喷奶水视频www网站免费| 亚洲成综合人影院在院播放| 国产成人3p视频免费观看| 青青操在线免费观看| 香蕉大伊亚洲人在线观看|