这是一个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
仅供参考学习
感谢原作者的辛苦工作