java應用程式中的聲音播放
阿新 • • 發佈:2019-02-05
這是一個java內建的不需要外部引入的庫檔案的用來在java應用程式中播放聲音的例子:
已取樣的音訊(Sampled Audio):已取樣音訊以聲波振幅的時間取樣資料序列的形式表示。 javax.sound.sampled包含對該類的支援。支援的檔案格式有wav,au,和aiff。樣品可從8位元到16位元,取樣率可從8kHz到48kHz。
要播放取樣音訊,需要建立一個SourceDataLine或者Clip的例項, 而這個例項將作為軟體音訊混合器(audio mixer)的原始檔,然後音訊樣本被載入併發送給混合器(mixer)。混合器會混合各個樣本然後傳送給音效卡的音訊輸出。
1. 使用SourceDataLine
package myutils; 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; /** * * @author Yusata Infotech */ public class play_audio { public static void main(String[] args) { File file; AudioInputStream audio; AudioFormat format; SourceDataLine auline = null; DataLine.Info info; try { System.out.println("Start"); file = new File("d://wavs//beep-1.wav"); audio = AudioSystem.getAudioInputStream(file); format = audio.getFormat(); info = new DataLine.Info(SourceDataLine.class, format); auline = (SourceDataLine) AudioSystem.getLine(info); auline.open(format); auline.start(); int nBytesRead = 0; byte[] abData = new byte[524288]; while (nBytesRead != -1) { nBytesRead = audio.read(abData, 0, abData.length); if (nBytesRead >= 0) { auline.write(abData, 0, nBytesRead); } } } catch (IOException e) { // System.out.println(e.getMessage()); e.printStackTrace(); } catch (UnsupportedAudioFileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { auline.drain(); auline.close(); } } }
auline.drain()和auline.close()用來保證該聲音檔案播放完畢,如果去掉會出現聲音為播放完即結束的情況。
2. 使用Clip
package com.whongshe.utils; import java.io.*; import java.net.URL; import javax.sound.sampled.*; import javax.swing.*; // To play sound using Clip, the process need to be alive. // Hence, we use a Swing application. public class SoundClipTest extends JFrame { // Constructor public SoundClipTest() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Test Sound Clip"); this.setSize(300, 200); this.setVisible(true); try { // Open an audio input stream. // URL url = this.getClass().getResource("hello.wav"); File file = new File("bin/com/whongshe/utils/hello.wav"); // AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); AudioInputStream audioIn = AudioSystem.getAudioInputStream(file); // Get a sound clip resource. Clip clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } } public static void main(String[] args) { new SoundClipTest(); } }
通過Clip播放聲音的步驟:
1. 從一個聲音檔案或URL分配一個AudioInputStream管道。
// from a wave File
File soundFile = new File("eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
// from a URL
URL url = new URL("http://www.zzz.com/eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// can read from a disk file and also a file contained inside a JAR (used for distribution)
// recommended
URL url = this.getClass().getClassLoader().getResource("eatfood.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
2. 通過呼叫靜態方法AudioSystem.getClip();獲取一個Clip例項
Clip clip = AudioSystem.getClip();
3. 開啟clip,裝載從上面得到的AudioInputStream
clip.open(audioIn);
// For small-size file only. Do not use this to open a large file over slow network, as it blocks.
4. 現在可以通過呼叫start()或者loop()來播放clip
// start()
clip.start(); // play once
// Loop()
clip.loop(0); // repeat none (play once), can be used in place of start().
clip.loop(5); // repeat 5 times (play 6 times)
clip.loop(Clip.LOOP_CONTINUOUSLY); // repeat forever
5. 可通過呼叫stop()方法來停止播放,或者停止迴圈播放
if (clip.isRunning()) clip.stop();
以上內容來自
1. http://aboutyusata.blogspot.com/2012/10/play-soundwav-audio-in-java.html
2. http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
僅供參考學習
感謝原作者的辛苦工作