Meaningless Drivel is fun!*
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
Author

Java Applet & Sound

Rick Katka
Greenhorn

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
getContentPane().add(jbutton_play,BorderLayout.WEST);

//add text to the button
jbutton_play.setText("PLAY");

//add an action listener to the button
jbutton_play.addActionListener(this);
////////////////////////////////////////////////////

//////////////////pause button//////////////////////
//adds the play button to the left side of the applet
getContentPane().add(jbutton_pause,BorderLayout.CENTER);

//add text to the button
jbutton_pause.setText("PAUSE");

//add an action listener to the button
jbutton_pause.addActionListener(this);
////////////////////////////////////////////////////

//////////////////stop button//////////////////////
//adds the play button to the left side of the applet
getContentPane().add(jbutton_stop,BorderLayout.EAST);

//add text to the button
jbutton_stop.setText("STOP");

//add an action listener to the button
jbutton_stop.addActionListener(this);
////////////////////////////////////////////////////

//////////////////Search button//////////////////////
//adds the play button to the left side of the applet
getContentPane().add(jbutton_search,BorderLayout.SOUTH);

//add text to the button
jbutton_search.setText("SEARCH");

//add an action listener to the button
jbutton_search.addActionListener(this);
////////////////////////////////////////////////////


//set size
setSize(250,100);
//set visible
setVisible(true);

}//end init constructor

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("SEARCH") )
{
context.SEARCH();
}
if(e.getActionCommand().equals("PLAY") )
{
context.PLAY();
}
else if(e.getActionCommand().equals("PAUSE") )
{
context.PAUSE();
}
else if(e.getActionCommand().equals("STOP") )
{
context.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()
{
sound1.play();//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: 15300
    
    6

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: http://aspose.com/file-tools
 
subject: Java Applet & Sound