当前位置: 代码迷 >> J2SE >> 怎么在java application中播放声音
  详细解决方案

怎么在java application中播放声音

热度:50   发布时间:2016-04-23 22:14:33.0
如何在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{
  相关解决方案