Şaban Kaya
Şaban Kaya

Reputation: 41

JavaFX IllegalArgumentException

I'm trying to make buttons that when clicked they will play sound. But i have a trouble. I can't figure out, exactly. Here is my code:

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class Ms extends Application {
    @Override
    public void start(Stage primaryStage) {

        VBox vbox = new VBox(50);
        vbox.setOnScroll(null);

        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");
        Button button4 = new Button("4");
        Button button5 = new Button("5");
        Button button6 = new Button("6");
        HBox.setHgrow(button1, Priority.ALWAYS);
        HBox.setHgrow(button2, Priority.ALWAYS);
        button1.setPrefSize(367, 70);
        button2.setPrefSize(367, 70);
        button3.setPrefSize(367, 70);
        button4.setPrefSize(367, 70);
        button5.setPrefSize(367, 70);
        button6.setPrefSize(367, 70);
        button1.setOnMouseClicked(new EventHandler<Event>() {

            @Override
            public void handle(Event event) {
                Media hit = new Media("/Users/ErcumentKaya/Desktop/denek.mp3");
                MediaPlayer mediaPlayer = new MediaPlayer(hit);
                mediaPlayer.play();

            }
        });
        vbox.setSpacing(0);
        vbox.getChildren().addAll(button1, button2, button3, button4, button5, button6);

        ScrollPane scrollPane = new ScrollPane(vbox);

        BorderPane root = new BorderPane(scrollPane);
        root.setPadding(new Insets(15));

        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

My error message is

Exception in thread "JavaFX Application Thread"
java.lang.IllegalArgumentException: uri.getScheme() == null! uri ==
'/Users/ErcumentKaya/Desktop/denek.mp3'
      at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
      at javafx.scene.media.Media.<init>(Media.java:391)
      at Ms$1.handle(Ms.java:42)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
      at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)

Upvotes: 0

Views: 1244

Answers (2)

Marian
Marian

Reputation: 441

This worked for me:

File f = new File("path\\to\\file.mp3");
Media hit = new Media(f.toURI().toString());

It should help prevent syntax errors for specifying a URI string value for a local file.

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86754

This is covered in the Javadoc:

public Media(java.lang.String source)

Constructs a Media instance. This is the only way to specify the media source. The source must represent a valid URI and is immutable. Only HTTP, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown.

The URL must be a file: URL.

Upvotes: 3

Related Questions