使用Audio Player来播放wav文件

代码真心简单

import java.io.FileInputStream;
import java.io.IOException;

import sun.audio.AudioPlayer;

/**
 *
 * @author ztk
 */
public class NumAudioPlay {
        
    private String path = "src/com/numaudioplay/";
    
    public NumAudioPlay()
    {
        
    }
    public static void main(String args[])
            throws IOException
    {
        try {
        NumAudioPlay ap = new NumAudioPlay();
            ap.play("0.wav");
            Thread.sleep(1000);
            ap.play("1.wav");
            Thread.sleep(1000);
            ap.play("2.wav");
            Thread.sleep(1000);
            ap.play("3.wav");
            Thread.sleep(1000);
            ap.play("4.wav");
            Thread.sleep(1000);
            ap.play("5.wav");
            Thread.sleep(1000);
            ap.play("6.wav");
            Thread.sleep(1000);
            ap.play("7.wav");
            Thread.sleep(1000);
            ap.play("8.wav");
            Thread.sleep(1000);
            ap.play("9.wav");
            Thread.sleep(1000);
        } catch (Exception ex){
            ex.printStackTrace();
        }      
    }
    
    public void play(String wavName)
    {
        final String wavPath = path + wavName;
        /*Runnable runnable = new Runnable() {
            public void run() {
                try{
                    AudioPlayer.player.start(new FileInputStream(wavPath));
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();   */  
        
        try{
            AudioPlayer.player.start(new FileInputStream(wavPath));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }            
}

你可能感兴趣的:(Java相关)