`
- 浏览:
224362 次
- 性别:
- 来自:
杭州
-
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
<html>
<body>
<applet code=SoundExample.class width=450 height=50>
</applet>
</body>
</html>
*/
/*
* 1.1 version.
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
public class SoundExample extends Applet implements ActionListener {
SoundList soundList;
String onceFile = "bark.au";
String loopFile = "train.au";
AudioClip onceClip;
AudioClip loopClip;
Button playOnce;
Button startLoop;
Button stopLoop;
Button reload;
boolean looping = false;
public void init() {
playOnce = new Button("Bark!");
playOnce.addActionListener(this);
add(playOnce);
startLoop = new Button("Start sound loop");
stopLoop = new Button("Stop sound loop");
stopLoop.setEnabled(false);
startLoop.addActionListener(this);
add(startLoop);
stopLoop.addActionListener(this);
add(stopLoop);
reload = new Button("Reload sounds");
reload.addActionListener(this);
add(reload);
startLoadingSounds();
}
void startLoadingSounds() {
//Start asynchronous sound loading.
soundList = new SoundList(this, getCodeBase());
soundList.startLoading(loopFile);
soundList.startLoading(onceFile);
}
public void stop() {
onceClip.stop(); //Cut short the one-time sound.
if (looping) {
loopClip.stop(); //Stop the sound loop.
}
}
public void start() {
if (looping) {
loopClip.loop(); //Restart the sound loop.
}
}
public void actionPerformed(ActionEvent event) {
//PLAY BUTTON
Object source = event.getSource();
if (source == playOnce) {
if (onceClip == null) {
//Try to get the AudioClip.
onceClip = soundList.getClip(onceFile);
}
if (onceClip != null) { //If the sound is loaded:
onceClip.play(); //Play it once.
showStatus("Playing sound " + onceFile + ".");
} else {
showStatus("Sound " + onceFile + " not loaded yet.");
}
return;
}
//START LOOP BUTTON
if (source == startLoop) {
if (loopClip == null) {
//Try to get the AudioClip.
loopClip = soundList.getClip(loopFile);
}
if (loopClip != null) { //If the sound is loaded:
looping = true;
loopClip.loop(); //Start the sound loop.
stopLoop.setEnabled(true); //Enable stop button.
startLoop.setEnabled(false); //Disable start button.
showStatus("Playing sound " + loopFile + " continuously.");
} else {
showStatus("Sound " + loopFile + " not loaded yet.");
}
return;
}
//STOP LOOP BUTTON
if (source == stopLoop) {
if (looping) {
looping = false;
loopClip.stop(); //Stop the sound loop.
startLoop.setEnabled(true); //Enable start button.
stopLoop.setEnabled(false); //Disable stop button.
}
showStatus("Stopped playing sound " + loopFile + ".");
return;
}
//RELOAD BUTTON
if (source == reload) {
if (looping) { //Stop the sound loop.
looping = false;
loopClip.stop();
startLoop.setEnabled(true); //Enable start button.
stopLoop.setEnabled(false); //Disable stop button.
}
loopClip = null; //Reset AudioClip to null.
onceClip = null; //Reset AudioClip to null.
startLoadingSounds();
showStatus("Reloading all sounds.");
return;
}
}
}
/*
* Code is the same in both 1.0 and 1.1.
*/
//Loads and holds a bunch of audio files whose locations are specified
//relative to a fixed base URL.
class SoundList extends java.util.Hashtable {
Applet applet;
URL baseURL;
public SoundList(Applet applet, URL baseURL) {
super(5); //Initialize Hashtable with capacity of 5 entries.
this.applet = applet;
this.baseURL = baseURL;
}
public void startLoading(String relativeURL) {
new SoundLoader(applet, this, baseURL, relativeURL);
}
public AudioClip getClip(String relativeURL) {
return (AudioClip) get(relativeURL);
}
public void putClip(AudioClip clip, String relativeURL) {
put(relativeURL, clip);
}
}
/*
* Code is the same in both 1.0 and 1.1.
*/
class SoundLoader extends Thread {
Applet applet;
SoundList soundList;
URL baseURL;
String relativeURL;
public SoundLoader(Applet applet, SoundList soundList, URL baseURL,
String relativeURL) {
this.applet = applet;
this.soundList = soundList;
this.baseURL = baseURL;
this.relativeURL = relativeURL;
setPriority(MIN_PRIORITY);
start();
}
public void run() {
AudioClip audioClip = applet.getAudioClip(baseURL, relativeURL);
//AudioClips load too fast for me!
//Simulate slow loading by adding a delay of up to 10 seconds.
try {
sleep((int) (Math.random() * 10000));
} catch (InterruptedException e) {
}
soundList.putClip(audioClip, relativeURL);
}
}
*/
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1 AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2 AudioClip chosenClip = sound1; //选择的声音剪辑...
When running the Java Sound demo as an applet these permissions are necessary in order to load/save files and record audio : grant { permission java.io.FilePermission "<<ALL FILES>>", "read, write...
Applet Programming With the Java™ Sound API
play sound clip in java can be used in applet or window
AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1 AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2 AudioClip chosenClip = sound1; //选择的...
not an applet showing a penguin waving its flipper. You’ve done an introductory course on Java, so you understand about classes, objects, inheritance, exception handling, threads, and basic graphics....
AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1 AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2 AudioClip chosenClip = sound1; //选择的声音剪辑...
Day 8 Java Applet Basics 129 9 Graphics, Fonts, and Color 149 10 Simple Animation and Threads 173 11 More Animation, Images, and Sound 195 12 Managing Simple Events and Interactivity 217 13 User ...
在本教程中,您将会了解如何使用 Java Sound API 将音频集成到您的applet 和应用程序。除了向您讲解数字音频的基本知识以外,本教程还将向您演示如何: 播放音频 录制音频 混合音频 循环播放音频 执行简单的数字...
Java图形编程:AWT(Java2D、JavaSound、Media)、Swing、SWT、Jface。Java网络编程:Applet、Socket/TCP/UDP、NIO、RMI、CORBA。Java高级特性:反射、泛型、注释符、自动装箱和拆箱、枚举类、可变参数、可变返回...
Java图形编程:AWT(Java2D、JavaSound、Media)、Swing、SWT、Jface。Java网络编程:Applet、Socket/TCP/UDP、NIO、RMI、CORBA。Java高级特性:反射、泛型、注释符、自动装箱和拆箱、枚举类、可变参数、可变返回...
Java图形编程:AWT(Java2D、JavaSound、Media)、Swing、SWT、Jface。Java网络编程:Applet、Socket/TCP/UDP、NIO、RMI、CORBA。Java高级特性:反射、泛型、注释符、自动装箱和拆箱、枚举类、可变参数、可变返回...
Java图形编程:AWT(Java2D、JavaSound、Media)、Swing、SWT、Jface。Java网络编程:Applet、Socket/TCP/UDP、NIO、RMI、CORBA。Java高级特性:反射、泛型、注释符、自动装箱和拆箱、枚举类、可变参数、可变返回...
AudioClip sound = Applet.newAudioClip(new File("sounds/背景音乐.wav").toURL()); 但是由于只支持wav格式的音乐,.wav这种格式是无损音乐。 输出举例 不同的图片块通过点击产生移动,若一个图片块周围都是与其...
static Sound_Play_File sound=new Sound_Play_File(); /*-------------------构造函数----------------------*/ public MyFrame(){ super(); this.setLayout(null); this.setSize(WIDTH,HEITH);//...
Accessibility Drag n Drop Input Methods Image I/O Print Service Sound Java SE API Integration Libraries IDL JDBC JNDI RMI RMI-IIOP Scripting Other Base Libraries Beans Int'l Support Input/Output...
Java图形编程:AWT(Java2D、JavaSound、Media)、Swing、SWT、Jface。Java网络编程:Applet、Socket/TCP/UDP、NIO、RMI、CORBA。Java高级特性:反射、泛型、注释符、自动装箱和拆箱、枚举类、可变参数、可变返回...
java.applet java.awt java.awt.color java.awt.datatransfer java.awt.dnd java.awt.event java.awt.font java.awt.geom java.awt.im java.awt.im.spi java.awt.image java.awt.image.renderable java....