zoukankan      html  css  js  c++  java
  • Sound (audio file) player in java

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/sound-audio-file-player-in-java-working.html

    Sound (audio file) player in java - working source code example

     import java.io.File;  
     import java.io.IOException;  
     import javax.sound.sampled.AudioFormat;  
     import javax.sound.sampled.AudioInputStream;  
     import javax.sound.sampled.AudioSystem;  
     import javax.sound.sampled.DataLine;  
     import javax.sound.sampled.LineUnavailableException;  
     import javax.sound.sampled.SourceDataLine;  
     import javax.sound.sampled.UnsupportedAudioFileException;  
     public class Player implements Runnable {  
          static final int BUF_SIZE = 16384;  
          private AudioInputStream audioInputStream;  
          private AudioFormat format;  
          SourceDataLine line;  
          Thread thread;  
          public Player(AudioInputStream audioInputStream) {  
               this.audioInputStream = audioInputStream;  
               format = audioInputStream.getFormat();  
          }  
          public Player(File wavFile) throws UnsupportedAudioFileException, IOException {  
               this.audioInputStream = AudioSystem.getAudioInputStream(wavFile);  
               format = audioInputStream.getFormat();  
          }  
          public void start() {  
               thread = new Thread(this);  
               thread.setName("Playback");  
               thread.start();  
          }  
          public void stop() {  
               thread = null;  
          }  
          private void shutDown(final String message) {  
               if (thread != null) {  
                    thread = null;  
               }  
               System.out.println(message);  
          }  
          @Override  
          public void run() {  
               // make sure we have something to play  
               if (audioInputStream == null) {  
                    shutDown("No loaded audio to play back");  
                    return;  
               }  
               // get an AudioInputStream of the desired format for playback  
               final AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream);  
               if (playbackInputStream == null) {  
                    shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format);  
                    return;  
               }  
               line = getSourceDataLineForPlayback();  
               // play back the captured audio data  
               final int frameSizeInBytes = format.getFrameSize();  
               final int bufferLengthInFrames = line.getBufferSize() / 8;  
               final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;  
               final byte[] audioBuffer = new byte[bufferLengthInBytes];  
               int numBytesRead = 0;  
               // start the source data line  
               line.start();  
               while (thread != null) {  
                    try {  
                         if ((numBytesRead = playbackInputStream.read(audioBuffer)) == -1) {  
                              break;  
                         }  
                         int numBytesRemaining = numBytesRead;  
                         while (numBytesRemaining > 0) {  
                              numBytesRemaining -= line.write(audioBuffer, 0, numBytesRemaining);  
                         }  
                    } catch (final Exception e) {  
                         shutDown("Error during playback: " + e);  
                         break;  
                    }  
               }  
               // stop and close the line.  
               if (thread != null) {  
                    line.drain();  
               }  
               line.stop();  
               line.close();  
               line = null;  
               thread = null;  
          }  
          private SourceDataLine getSourceDataLineForPlayback() {  
               SourceDataLine line;  
               final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);  
               if (!AudioSystem.isLineSupported(info)) {  
                    return null;  
               }  
               // get and open the source data line for playback.  
               try {  
                    line = (SourceDataLine) AudioSystem.getLine(info);  
                    line.open(format, BUF_SIZE);  
               } catch (final LineUnavailableException ex) {  
                    return null;  
               }  
               return line;  
          }  
     }  

    Testing the wave sound player

     public class Testss {  
          public static void main(String[] args) throws Exception {  
               Player pl = new Player(new File("filename.wav"));  
               pl.start();  
               Thread.sleep(2000);  
          }  
     }  
  • 相关阅读:
    JS中的prototype
    Php5.3的lambda函数以及closure(闭包)
    JavaScript事件委托的技术原理
    css 里层元素撑不开外层元素
    扩展VirtualBox虚拟机磁盘容量
    easyUI 条件查询 跟分页数据展示写在了一起的
    (转)Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
    有想去北京工作的的想法了
    第一次写oracle SQL 两个表链接查询
    第三天 SQL小记
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11888700.html
Copyright © 2011-2022 走看看