首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么在java application中播放声音

2013-06-19 
如何在java application中播放声音如何在java application中播放声音,求大神赐教。。[解决办法]public class

如何在java application中播放声音
如何在java application中播放声音,求大神赐教。。
[解决办法]

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);
  }
}

[解决办法]

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();
}
}

热点排行
Bad Request.