java怎么播放不了声音,怎么在java application中播放声音
如何在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{
總結
以上是生活随笔為你收集整理的java怎么播放不了声音,怎么在java application中播放声音的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中oracle.链接,java连
- 下一篇: matlab如何创建table,MATL