• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Java Applet & Sound

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic