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?
 
vladi21Connect With a Mentor Commented:
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.