Reputation: 11
Hi I have a Java Applet which plays a file of my local file system. When I embed the Applet in a web page I get a io.FilePermission Exception. I understand that the only way round this is to get my Applet signed which I don't wish to.
How would I go about read the file from it's own jar file?, As I understand this will be the best way.
Thanks for any help guys.
Heres my code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PlaySoundsApplet extends Applet implements ActionListener{
Button play,stop;
AudioClip audioClip;
public void init(){
play = new Button(" Play in Loop ");
add(play);
play.addActionListener(this);
stop = new Button(" Stop ");
add(stop);
stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), "clip.wav"); //Exception here trying to read from the www root instead of jar file
}
public void actionPerformed(ActionEvent ae){
Button source = (Button)ae.getSource();
if (source.getLabel() == " Play in Loop "){
audioClip.play();
}
else if(source.getLabel() == " Stop "){
audioClip.stop();
}
}
}
Updated code:
public class PlaySoundsApplet extends Applet implements ActionListener{
Button play,stop;
AudioClip audioClip;
public void init(){
play = new Button(" Play in Loop ");
add(play);
play.addActionListener(this);
stop = new Button(" Stop ");
add(stop);
stop.addActionListener(this);
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("resources/clip.wav"));
Clip audioClip = AudioSystem.getClip();
audioClip.open(audioInputStream);
} catch (UnsupportedAudioFileException e1) {System.out.println("Unsoported Error");}
catch (IOException e1) {System.out.println("IO Error");} catch (LineUnavailableException e) {System.out.println("Line unavailable Error");}
}
public void actionPerformed(ActionEvent ae){
Button source = (Button)ae.getSource();
if (source.getLabel() == " Play in Loop "){
audioClip.play();
}
else if(source.getLabel() == " Stop "){
audioClip.stop();
}
}
}
Upvotes: 1
Views: 292
Reputation: 46881
Try
URL url = getDocumentBase();
AudioClip audioClip = getAudioClip(url, "music/JButton.wav")
Project sturcture
Upvotes: 2