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);  
          }  
     }  
  • 相关阅读:
    一道C#基础题,看你能多长时间做出来?
    终于能在这里安家了
    你知道返回多少吗?(使用Math类)
    关于implicit和explicit关键词的用法
    关于基类与派生类的学习
    js控制输入框
    Oracle 动态SQL返回单条结果和结果集 转帖
    定时器:.NET Framework类库中的Timer类比较(转帖)
    UVA10020 Minimal coverage
    UVA1388 Graveyard
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11888700.html
Copyright © 2011-2022 走看看