无法使用AudioClip与Javafx一起使用

时间:2018-02-20 18:27:16

标签: java audio javafx audioclip

我试图让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);
    }
}

0 个答案:

没有答案