Trying out the audio clip

1k views Asked by At

Whenever I try the audio clip, I get this error:

java.net.MalformedURLException: no protocol: /Users/videogames/Documents/workspace/TryApplets/res/adv.wav---------       

What's the problem? Here's the code for the program. (I use a mac, if that matters at all)

package game;

import java.applet.*;
import java.net.*;

public class sound {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
        URL url = new URL("/Users/videogames/Documents/workspace/TryApplets/res/adv.wav");
        AudioClip clip = Applet.newAudioClip(url);
        clip.play();
        } catch (MalformedURLException murle) {
        System.out.println(murle);
        }
    }
}
2

There are 2 answers

0
Andrew Thompson On

An URL must start with something like http://.. or file://... The URL shown does not, it is not a valid URL.

3
Scary Wombat On

Valid protocols for the URL class are

 http, https, ftp, file, and jar

so try

URL url = new URL("file://Users/videogames/Documents/workspace/TryApplets/res/adv.wav");

If in doubt read the API

http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#URL(java.lang.String)