Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
public void soundKlatschen(){
try {
System.out.println("Sound abspielen wird aufgerufen");
AudioClip ac = Applet.newAudioClip (new File ("Sound/Applaus.wav").toURL());
ac.play();
} catch(Exception e) {
e.printStackTrace();
}
}
Applet#newAudioClip(java.net.URL)
@Fab1: Hast du deinen Code getestet? Ich glaube die Verwendung von File in einem Applet wird eine AccessControlException provozieren, weil dabei auf der Client-Festplatte gesucht wird.
In einem Applet besser immer über die CodeBase die Resourcen laden.
<Applet>.getAudioClip(getCodeBase(), "relPath/my.wav");
<Applet>.getAudioClip(getDocumentBase(), "relPath/my.wav");
public void init(){
button = new Button("Nippel1");
add(button);
try {
AudioClip sound1 = Applet.newAudioClip (new File ("/home/timo/workspace/Hallo2/Nippelboard/nippelsounds/nippelneu1_watt.wav").toURL());
sound1.play();
} catch(Exception e) {
e.printStackTrace();
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;
public class SoundApplet extends JApplet implements ActionListener {
private AudioClip clip;
public void init() {
setLayout(new GridBagLayout());
try {
clip = newAudioClip(new URL(getCodeBase()+"clip.wav"));
}
catch(MalformedURLException e) {
e.printStackTrace();
}
JButton playBtn = new JButton("Play");
playBtn.addActionListener(this);
add(playBtn);
}
public void actionPerformed(ActionEvent e) {
clip.play();
}
}
<html>
<title>SoundApplet</title>
<body>
<applet code="SoundApplet.class" width="400" height="300">
</applet>
</body>
</html>
public class Nippel
extends Button
{
private final AudioClip ac;
public Button(String name, AudioClip ac)
{
super(name);
if(null == ac) {
throw new IllegalArgumentException("AudioClip may not be null");
}
this.ac = ac;
addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(this == e.getSource()) {
ac.play();
}
}
}
}
}
Nippel n = new Nippel("Nippel1", getAudioClip(getCodeBase(), "relPath/my.wav"));
Dort wird das Ergebnis des getCodeBase()-Aufrufs (eine relative URL zum Applet) in eine neue URL gepackt, die Bezeichnung der zu ladenen Datei angehängt und damit der AudioClip geladen.[edit]@L-ectron-X: Zeile 14 in deinem Code... was ist das????:L ... okay vergiss das 😉[/edit]
<Applet>.add(/*(Button)*/ nippel1);