File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Java Applet & Sound Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Java Applet & Sound" Watch "Java Applet & Sound" New topic

Java Applet & Sound

Rick Katka

Joined: Feb 22, 2004
Posts: 7
I'm implementing a java applet that will play a sound file that is of a .wav format. I'm using the state design pattern and I am having a really difficult time getting my audio clips to play. I keep getting a NullPointerException when I call getDocumentBase(). Here is the Code that I have:

//Rick Katka
//May 4, 2005
//CSCI 471-Final Project
//JApplet wav file media player

///////////////////Needed for GUI//////////////////////
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.Applet.*;

////////////////////////Applet Driver///////////////////////
public class JApplet_MediaPlayer extends JApplet implements ActionListener
//references to buttons in the GUI

private JButton jbutton_play, jbutton_pause, jbutton_stop;
private JButton jbutton_search;
private MediaPlayer_LoadingState ls;
private AudioClip sound1;//wave file variable

//creates a new reference to the context class
MediaPlayer_Context context = new MediaPlayer_Context();

//init like constructor
public void init()
//super("Wave MediaPlayer");

//create a new instance of all buttons
jbutton_play = new JButton();
jbutton_pause = new JButton();
jbutton_stop = new JButton();
jbutton_search = new JButton();

//sets the applets layout
getContentPane().setLayout(new BorderLayout(3,3) );

///////////////////play button/////////////////////
//adds the play button to the left side of the applet

//add text to the button

//add an action listener to the button

//////////////////pause button//////////////////////
//adds the play button to the left side of the applet

//add text to the button

//add an action listener to the button

//////////////////stop button//////////////////////
//adds the play button to the left side of the applet

//add text to the button

//add an action listener to the button

//////////////////Search button//////////////////////
//adds the play button to the left side of the applet

//add text to the button

//add an action listener to the button

//set size
//set visible

}//end init constructor

public void actionPerformed(ActionEvent e)
if(e.getActionCommand().equals("SEARCH") )
if(e.getActionCommand().equals("PLAY") )
else if(e.getActionCommand().equals("PAUSE") )
else if(e.getActionCommand().equals("STOP") )

}//end action performed

public void setSound(AudioClip s)
sound1 = s;

public void setMyAudioClip(String s)
System.out.println("Here in setMyAudioClip " + s);
//***********MY ERROR IS HERE*******************
sound1 = getAudioClip (getDocumentBase(), s );

public void playDaSounds()
{;//plays the sound
}//end JApplet_MediaPlayer

Thanks for Any Help!! I need it. This is really bugging me.

Rick J. Katka
Sophomore, Carthage College
[ May 16, 2004: Message edited by: Gregg Bolinger ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

First, I edited your post and removed Urgent from the subject line. There are no posts here more important or urgent than the next persons, so no need to shout.

Secondly, this forum is for general questions about java for beginners. Your question is a little more specific so I am going to move it to the Applets forum where those guys can help you out.

GenRocket - Experts at Building Test Data
I agree. Here's the link:
subject: Java Applet & Sound
It's not a secret anymore!