注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

千鸟

本blog所有日志均系原创 转载请注明出处

 
 
 

日志

 
 

java 音效,笔记1  

2007-03-25 22:21:25|  分类: J2SE |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

播放声音的一般步骤:

  1打开声音文件:

    File f=new File("abc.wav");

    AudioInputStream stream = AudioSystem.getAudioInputStream(f);//打开输入流

    AudioFormat format= stream.getFormat();  //获得声音格式

//AudioFormat:是在声音流中指定特定数据安排的类。通过检查以音频格式存储的信息,可以发现在二进制

//声音数据中解释位的方式。

2.生成Line.    下面用line的子接口SourceDataLine,他可以将声音装入声音系统中Dine.Info的子类DataLine.Info.可以生成Line.

DataLine.Info主要作用

  • 受数据行支持的音频格式
  • 其内部缓冲区的最小和最大大小

      DataLine.Info info =new DataLine.Info(SourceDataLine.class, format);

      SourceDataLine line=(SourceDataLine)AudioSystem.getLine(info); 

      line.open(format, bufferSize);   //打开具有指定格式和建议缓冲区大小的行,这样可使该行获得所有所需系统资源并变得可操作.bufferSize:缓冲区大小int型.

    3.播放line 

       line.start();//允许某一数据行执行数据 I/O

      //通过一个循环执行下面的语句,就可以连续的播放声音了.当然如果要中断声音,加入一个判断标志,再break就行了

       line.write(buffer, 0, numBytesRead) // 将缓冲区内音频数据写入混频器,开始听到声音. buffer缓冲区,里面是声音数据 类型: byte[];   numBytesRead:缓冲区大小 类型:int

     源代码如下:

     

    package ecom;
    import java.io.*;
    import javax.sound.sampled.*;

    /**
        The SimpleSoundPlayer encapsulates a sound that can be opened
        from the file system and later played.
    */
    public class SimpleSoundPlayer  {

        public static void main(String[] args) {
            // load a sound
            SimpleSoundPlayer sound =
                new SimpleSoundPlayer("../sounds/abc.wav"); 
               
          // File ac=new File("../good.log");
          
         
         //  System.out.print(ac.getAbsolutePath());
          
            // create the stream to play
            InputStream stream =
                new ByteArrayInputStream(sound.getSamples());

            // play the sound
            sound.play(stream);

            // exit
            System.exit(0);
        }

        private AudioFormat format;
        private byte[] samples;

        /**
            Opens a sound from a file.
        */
        public SimpleSoundPlayer(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();
            }
        }


        /**
            Gets the samples of this sound as a byte array.
        */
        public byte[] getSamples() {
            return samples;
        }


        /**
            Gets the samples from an AudioInputStream as an array
            of bytes.
        */
        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;
        }


        /**
            Plays a stream. This method blocks (doesn't return) until
            the sound is finished playing.
        */
        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);  // 构建100ms的缓冲区,以实现实时改变声音流
                              //format.getFrameSize()获取以帧每秒为单位的帧速率
            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();

        }

    }

     

  •   评论这张
     
    阅读(602)| 评论(0)
    推荐 转载

    历史上的今天

    评论

    <#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    页脚

    网易公司版权所有 ©1997-2017