java 播放音频

package com.sunray.express.core.util;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


public class PlayAudio1 {


static BufferedInputStream buf;


public static void start() // 这是通过流读取声音文件
{
try {
File dir = new File("E:\\music\\YH\\wav");// 这里是你所要播放文件的路径
File[] fs = dir.listFiles();
for (File file : fs) {
System.out.println(file.getName());
File fObject = new File(dir, file.getName());// 歌曲名,记住格式有限制
FileInputStream files = new FileInputStream(fObject);
buf = new BufferedInputStream(files);
AudioStream audio = new AudioStream(buf);
AudioPlayer.player.start(audio);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}


public void close() throws Exception {
buf.close();
}


public static void loop() {
// AudioPlayer.player.
}


public static void main(String[] args) {
start();
loop();
}


}

你可能感兴趣的:(java 播放音频)