62,615
社区成员
发帖
与我相关
我的任务
分享
package test.buyticket;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
/**
* @author xujsh(xjs250@163.com)
*
*/
public class SimplePlayer {
private static ExecutorService playSoundService = Executors.newFixedThreadPool(1);
private SimplePlayer(){
}
public static void play(String filename){
if(filename == null || filename.equals("")){
System.err.println("Wave file can not be empty!");
}
play(new File(filename));
}
public static void play(File soundFile){
try {
if(soundFile == null || !soundFile.exists()){
System.err.println("Wave file not found: " + soundFile);
}
InputStream soundStream = new FileInputStream(soundFile);
play(soundStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void play(InputStream soundStream){
if (soundStream == null) {
System.err.println("sound file error!" );
return;
}
PlayTask task = new PlayTask(soundStream);
playSoundService.execute(task);
}
public static void destroy(){
playSoundService.shutdown();
}
private static class PlayTask implements Runnable{
private InputStream soundStream;
public PlayTask(InputStream soundStream){
this.soundStream = soundStream;
}
public void run(){
try {
if(soundStream == null){
System.out.println("sound stream error");
return;
}
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundStream);
AudioFormat format = audioInputStream.getFormat();
DataLine.Info datalineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(datalineInfo);
sourceDataLine.open(format);
if (sourceDataLine.isControlSupported(FloatControl.Type.PAN)) {
sourceDataLine.getControl(FloatControl.Type.PAN);
}
sourceDataLine.start();
int nBytesRead = 0;
byte[] abData = new byte[128 * 1024];
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
sourceDataLine.write(abData, 0, nBytesRead);
}
sourceDataLine.drain();
sourceDataLine.close();
soundStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
InputStream in = SimplePlayer.class.getClassLoader().getResourceAsStream("fuck.wav");
SimplePlayer.play(in);
SimplePlayer.destroy();
}
}
public class JavaAudioPlaySoundExample
{
public static void main(String[] args)
throws Exception
{
// open the sound file as a Java input stream
String gongFile = "/Users/al/DevDaily/Projects/MeditationApp/resources/gong.au";
InputStream in = new FileInputStream(gongFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
}