<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

    主站蜘蛛池模板: 亚洲日韩精品一区二区三区无码| 久久精品国产亚洲7777| 国产亚洲精品国产福利在线观看| 亚洲精品国产精品国自产观看| 免费国产成人午夜在线观看| 麻豆狠色伊人亚洲综合网站| 国产美女无遮挡免费视频| 999zyz**站免费毛片| 91丁香亚洲综合社区| 国产亚洲精品看片在线观看 | 久久aa毛片免费播放嗯啊| 国产成人亚洲合集青青草原精品 | 亚洲人成网站在线播放影院在线| 无限动漫网在线观看免费| jizz中国免费| 亚洲人成自拍网站在线观看| 亚洲成在人线av| 国产资源免费观看| 13一14周岁毛片免费| 日韩大片免费观看视频播放| 亚洲图片校园春色| 亚洲乱色熟女一区二区三区丝袜| 成人免费无码大片a毛片软件| 永久免费A∨片在线观看| 苍井空亚洲精品AA片在线播放| 亚洲AV乱码一区二区三区林ゆな| 国产精品成人四虎免费视频| 亚洲视频免费在线看| 日批视频网址免费观看| 美女免费视频一区二区三区| 亚洲一区免费视频| 亚洲国产第一页www| 亚洲无码黄色网址| 免费国产成人高清在线观看麻豆| 91成人免费在线视频| 大地资源在线资源免费观看 | 在线观看黄片免费入口不卡| 看亚洲a级一级毛片| 亚洲精华国产精华精华液| 亚洲人成日本在线观看| 久久青青草原亚洲AV无码麻豆|