" java.lang.IllegalArgumentException: uri.getScheme() == null! " error using JavaFX to play audio


Question

I'm trying to play mp3 audio on Java, and one of the forms. I belive is JavaFX, and all goes OK, until I try to play the audio.

This is the code:

import javax.sound.sampled.AudioPermission;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;


public class fx_main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");


                Media m = new Media("/04.mp3");
                MediaPlayer mp = new MediaPlayer(m);

                mp.play();

            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

         Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

And when i press the button and try to load the sound file, appears this exception:

   Hello World!
    Glass detected outstanding Java exception at -[GlassViewDelegate sendJavaMouseEvent:]:src/com/sun/mat/ui/GlassViewDelegate.m:543
    Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: uri.getScheme() == null!
        at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:217)
        at javafx.scene.media.Media.<init>(Media.java:369)
        at fx_man$1.handle(fx_man.java:27)
        at fx_man$1.handle(fx_man.java:1)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
        at javafx.event.Event.fireEvent(Event.java:171)
        at javafx.scene.Node.fireEvent(Node.java:6866)
        at javafx.scene.control.Button.fire(Button.java:179)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
        at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
        at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
        at javafx.event.Event.fireEvent(Event.java:171)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3164)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
        at com.sun.glass.ui.View.notifyMouse(View.

java:924)

How can I fix the error? I want to play the audio file when I click?

1
6
4/3/2016 5:45:58 PM

Accepted Answer

The constructor for Media requires a URL, so you'd need something starting with http:// or file://

btn.setOnAction(e -> {
  Media m = new Media(Paths.get("04.mp3").toUri().toString());
  new MediaPlayer(m).play();
});`

See http://docs.oracle.com/javase/8/javafx/media-tutorial/simpleplayer.htm

It also helps that both Path and File objects can be converted to URLs.

15
9/24/2014 11:40:49 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon