我想做这样的事情:
java.applet.Applet.newAudioClip(songPath);
但是,我想要在其中执行此操作的应用程序是 JFrame。那么,任何人都可以在不扩展 Applet 的情况下使用它吗?
播放剪辑(来自Javasound 标签 wiki 页面):
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!");
}
});
}
}
(来源:安德鲁汤普森)