如何在applet之外使用newAudioClip?

时间:2013-08-27 09:02:21

标签: java audio applet jframe javasound

我想做这样的事情:

    java.applet.Applet.newAudioClip(songPath);

但是,我想要在其中执行的应用程序是JFrame。那么,任何人都可以在不扩展Applet的情况下使用它吗?

1 个答案:

答案 0 :(得分:0)

播放剪辑(来自Javasound tag wiki page):

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

(资料来源:Andrew Thompson