如何在java application中播放声音

风吹得好舒服 2013-05-17 01:11:29
如何在java application中播放声音,求大神赐教。。
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
若鱼1919 2013-05-19
  • 打赏
  • 举报
回复

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

风吹得好舒服 2013-05-18
  • 打赏
  • 举报
回复
引用 1 楼 AARON7744 的回复:
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);
  }
}
不能识别AudioStream这个类。。
-AJ- 2013-05-18
  • 打赏
  • 举报
回复
javax.sound.sampled.AudioInputStream
huntor 2013-05-17
  • 打赏
  • 举报
回复
javax.sound包
-AJ- 2013-05-17
  • 打赏
  • 举报
回复
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);
  }
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧