這是一個音頻播放的簡單程序,是對網上的一個程序改造的,是把它簡單化了。
其中的stop()、pause()函數都沒有使用。
該程序支持.wav格式的音頻,改掉主函數中的音頻名字,就可以直接運行。音頻播放完會自動關閉。
public class AudioPlay
implements Runnable {
?private JavaSoundAudioClip locate, warning;
?protected boolean threadExit;
?protected boolean stopped;
?protected boolean playing;
?Thread playerThread;
?public AudioPlay() {
?}
?public void start(File f) {
??playing = true;
??stopped = false;
??try {
???FileInputStream ff = new FileInputStream(f);
???locate = new JavaSoundAudioClip(ff);
??}
??catch (Exception e) {
???e.printStackTrace();
??}
??playerThread = new Thread(this);
??playerThread.start();
?}
?public void run() {
??if (playing) {
???if (!stopped) {
????if (locate == null)
?????System.out.print("nulll");
????try {
?????locate.play();
?????System.out.print("test");
????}
????catch (Exception ee) {
?????ee.printStackTrace();
????}
???}
??}
?}
?public void stop() {
??stopped = true;
??threadExit = true;
??if (playing == true) {
???playing = false;
???locate.stop();
??}
?}
?public void pause() {
??if (playing == true) {
???locate.stop();
??}
?}
?
?public static void main(String a[]){
??
??AudioPlay sound=new AudioPlay();
??try{
??
??File f=new File("Sound/Contagious.wav");//用適當的路徑和文件名替換
??sound.start(f);
??}catch(Exception e){
???e.printStackTrace();
??}
??
?}
}