user2333960
user2333960

Reputation: 7

Beginner Java Applet -- Cannot get sound file to playback

I am working on a beginner java project, and I want a sound to play right when you open the java applet. I have put an .au file in "C:\Program Files (x86)\BlueJ" -- this is where the bluej exe is (I figure that is the correct location for the file the way I call it in my code).

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;


public class guitarGame extends Applet implements ActionListener, KeyListener {
AudioClip brainStew; 
Timer timer = new Timer (1000, this);

public void init(){
    brainStew = getAudioClip(getDocumentBase(), "green day - brain stew.au");
    brainStew.play();

}

public void keyReleased(KeyEvent ae){}

public void keyPressed(KeyEvent ae){

    repaint();
}

public void keyTyped(KeyEvent ae){}

public void actionPerformed (ActionEvent ae){}
public void paint (Graphics g)
{   
}
}

Any help would be appreciated, thanks.

Upvotes: 0

Views: 1868

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168835

I have put an .au file in "C:\Program Files (x86)\BlueJ" -- this is where the bluej exe is (I figure that is the correct location for the file the way I call it in my code).

You figured wrong. Your 'program files' directory is not accessible to a server providing the applet page nor therefor the applet.

getAudioClip(getDocumentBase(), "green day - brain stew.au");

That will lead to the JVM looking for the resource in the same directory as the HTML.

While the method call might properly URL encode space characters, it might not as well, so this will be more reliable.

getAudioClip(getDocumentBase(), "green%20day%20-%20brain%20stew.au");

Upvotes: 1

Related Questions