<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 閱讀(1473) 評論(0)  編輯  收藏 所屬分類: corejava

    主站蜘蛛池模板: 亚洲三级高清免费| 久久久久免费精品国产| 97在线观免费视频观看| 亚洲黄色在线观看视频| 99在线观看视频免费| 亚洲专区在线视频| 国拍在线精品视频免费观看 | 亚洲色成人中文字幕网站 | 日韩在线观看免费| 亚洲国产日韩成人综合天堂| jzzjzz免费观看大片免费| 亚洲综合另类小说色区| 无码av免费一区二区三区| 亚洲经典在线中文字幕| 插B内射18免费视频| 豆国产96在线|亚洲| 亚洲人成无码www久久久| a级毛片100部免费观看| 亚洲视频在线观看网址| 在线观看免费毛片| eeuss免费天堂影院| 久久精品国产亚洲AV麻豆~| 久草视频免费在线| 麻豆91免费视频| 国产成人A人亚洲精品无码| 成年人免费的视频| 亚洲av成人片在线观看| 亚洲愉拍99热成人精品热久久| 无码人妻一区二区三区免费n鬼沢| 亚洲乱码一二三四区麻豆| 日韩免费观看视频| 嫩草在线视频www免费看| 国产精品亚洲精品青青青| vvvv99日韩精品亚洲| 久久久精品2019免费观看 | a级毛片在线免费看| 亚洲日产2021三区在线| | 亚洲图片在线观看| 国产精品无码免费视频二三区| baoyu116.永久免费视频|