0

I'm trying to get AudioClip to play a WAV file using my Professors animation template but I can't seem to get it to work. I get the following errors:

Exception in thread "Thread-4" java.lang.IllegalArgumentException: 

uri.getScheme() == null! uri == 'sounds/select.wav'
    at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
    at com.sun.media.jfxmediaimpl.NativeMediaAudioClip.<init>(NativeMediaAudioClip.java:53)
    at com.sun.media.jfxmediaimpl.NativeMediaAudioClip.load(NativeMediaAudioClip.java:63)
    at com.sun.media.jfxmediaimpl.AudioClipProvider.load(AudioClipProvider.java:66)

at com.sun.media.jfxmedia.AudioClip.load(AudioClip.java:135)

at javafx.scene.media.AudioClip.<init>(AudioClip.java:83)

at audioclip.FXAnimationTemplate.animate(FXAnimationTemplate.java:49)

at audioclip.FXAnimationTemplate.lambda$start$0(FXAnimationTemplate.java:39)

at java.lang.Thread.run(Thread.java:748)

The WAV file is in the project folder, as well as the source folder just to be safe. I used to code for AudioClip from Oracles website.

package audioclip;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.media.AudioClip;

/**
 * Use this template to create simple animations in FX. Change the name of the
 * class and put your own name as author below. Change the size of the canvas
 * and the window title where marked and add your drawing code in the animation
 * method where shown.
 *
 * @author YOUR NAME
 */
public class FXAnimationTemplate extends Application {

    /**
     * Sets up the stage and starts the main thread. Your drawing code should
     * NOT go here.
     *
     * @param stage The first stage
     */
    @Override
    public void start(Stage stage) {
        stage.setTitle("Section 1.4 Animated!"); // window title here
        Canvas canvas = new Canvas(400, 300); // canvas size here
        Group root = new Group();
        Scene scene = new Scene(root);
        root.getChildren().add(canvas);
        stage.setScene(scene);
        stage.show();
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // This code starts a "thread" which will run your animation
        Thread t = new Thread(() -> animate(gc));
        t.start();
    }

    /**
     * Animation thread. This is where you put your animation code.
     *
     * @param gc The drawing surface
     */
    public void animate(GraphicsContext gc) {
        AudioClip select = new AudioClip("sounds/select.wav");
        select.play();

    }

    /**
     * Use this method instead of Thread.sleep(). It handles the possible
     * exception by catching it, because re-throwing it is not an option in this
     * case.
     *
     * @param duration Pause time in milliseconds.
     */
    public static void pause(int duration) {
        try {
            Thread.sleep(duration);
        } catch (InterruptedException ex) {
        }
    }

    /**
     * Exits the app completely when the window is closed. This is necessary to
     * kill the animation thread.
     */
    @Override
    public void stop() {
        System.exit(0);
    }

    /**
     * Launches the app
     *
     * @param args unused
     */
    public static void main(String[] args) {
        launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
Zach
  • 59
  • 4
  • 1
    The [`AudioClip` constructor](https://docs.oracle.com/javase/9/docs/api/javafx/scene/media/AudioClip.html#AudioClip-java.lang.String-) is expecting a URL. As the error message states, there is no scheme for your URL. You need something like `getClass().getResource(...).toExternalForm()` as the argument. (The value you pass to `getResource(...)` depends on your project layout.) – James_D Feb 20 '18 at 18:38
  • Would I just use a wav off the internet and link to that? Am I understanding this correctly? – Zach Feb 20 '18 at 18:42
  • I thought you said the .wav file was part of the project? – James_D Feb 20 '18 at 18:43
  • I got it to work, thanks for the help James – Zach Feb 20 '18 at 18:46
  • If you figured it out, you should show the correct form for others who come to this thread with similar problems! You can either describe the answer by editing your question, or putting it in an answer (answering yourself, perhaps the most usual) or even putting it in a comment. Another option is deleting the question, since there is little here that is helpful if there is no answer posted. – Phil Freihofner May 14 '19 at 16:51

0 Answers0