How to include path to audio clip?

I'm trying to compile an example I saw in a book.  I'm having the following problem:  When I include the path to my audio clip as a string parameter, the comiler tells me it can't convert the string to java.net.URL.  If I create a URL using the string, the compiler tells me I can't throw the malformedUrlException in the init method.  So how do I do this? I have both versions of the code below.

code - first way:


// soundButton.java
//
// This applet is an example from a book I saw in the
// bookstore.  It plays an .au file when you click on
// the button.



/*
   <applet
   code="soundButton" width=200 height=200>
      </applet>
*/
 

  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
 
  public class soundButton extends Applet implements ActionListener
  {  
     // a sound file
     AudioClip sound;
                                                                 
     public void init()
     {
        // add URL to this
        sound = getAudioClip("http://www.netcom.com/~me/www/images/aufile.au");
        Button button = new Button("Press for Sound");
        addActionListener(this);
        add(button);
     }
     
     public void actionPerformed(ActionEvent e)
     {
        if (sound != null)
        sound.play();
     }
   }
   

code - second way:



// soundButton.java
//
// This applet is an example from a book I saw in the
// bookstore.  It plays an .au file when you click on
// the button.



/*
   <applet
   code="soundButton" width=200 height=200>
      </applet>
*/
 

  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
import java.net.URL;
 
  public class soundButton extends Applet implements ActionListener
  {  
     // a sound file
     AudioClip sound;

    URL clipURL = new URL("http://www.netcom.com/~me/www/images/aufile.au");
                                                                 
     public void init()
     {
        // add URL to this
        sound = getAudioClip(clipURL);
        Button button = new Button("Press for Sound");
        addActionListener(this);
        add(button);
     }
     
     public void actionPerformed(ActionEvent e)
     {
        if (sound != null)
        sound.play();
     }
   }
   
 
 
 
 
 
 
 
 
 
 
 
 
ardasheksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vladi21Commented:
only 2 variants:

    /**
     * Returns the <code>AudioClip</code> object specified by the
     * <code>URL</code> argument.
     * <p>
     * This method always returns immediately, whether or not the audio
     * clip exists. When this applet attempts to play the audio clip, the
     * data will be loaded.
     *
     * @param   url  an absolute URL giving the location of the audio clip.
     * @return  the audio clip at the specified URL.
     * @see     java.applet.AudioClip
     * @since   JDK1.0
     */
    public AudioClip getAudioClip(URL url) {
      return getAppletContext().getAudioClip(url);
    }

    /**
     * Returns the <code>AudioClip</code> object specified by the
     * <code>URL</code> and <code>name</code> arguments.
     * <p>
     * This method always returns immediately, whether or not the audio
     * clip exists. When this applet attempts to play the audio clip, the
     * data will be loaded.
     *
     * @param   url    an absolute URL giving the base location of the
     *                 audio clip.
     * @param   name   the location of the audio clip, relative to the
     *                 <code>url</code> argument.
     * @return  the audio clip at the specified URL.
     * @see     java.applet.AudioClip
     * @since   JDK1.0
     */
    public AudioClip getAudioClip(URL url, String name) {
      try {
          return getAudioClip(new URL(url, name));
      } catch (MalformedURLException e) {
          return null;
      }
    }

0
vladi21Commented:
import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;

  public class soundButton extends Applet implements ActionListener
  {
     // a sound file
     AudioClip sound;

     public void init()
     {
        // add URL to this
        try{
        sound = getAudioClip(new java.net.URL("http://www.netcom.com/~me/www/images/aufile.au"));
        }catch(java.net.MalformedURLException e){e.printStackTrace();}
        Button button = new Button("Press for Sound");
        button.addActionListener(this);
        add(button);
     }

     public void actionPerformed(ActionEvent e)
     {
        if (sound != null)
        sound.play();
     }
   }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ardasheksAuthor Commented:
Thank you. This got me on the right track.
I ended up using

sound = getAudioClip(new
           java.net.URL(getCodeBase() + "file.au"));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.