0

我正在尝试让 AudioClip 使用我的教授动画模板播放 WAV 文件,但我似乎无法让它工作。我收到以下错误:

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)

WAV 文件位于项目文件夹以及源文件夹中,以确保安全。我曾经在 Oracles 网站上为 AudioClip 编写代码。

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);
    }
}
4

0 回答 0