Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 581
Adrian Garcia Avatar asked Sep 24 '14 23:09

Adrian Garcia


2 Answers

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.

like image 82
Costlow Avatar answered Nov 10 '22 06:11

Costlow


You can also try:

Media letterSound = new Media(new File(sound).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(letterSound);
mediaPlayer.play();
like image 1
Amiothenes Avatar answered Nov 10 '22 08:11

Amiothenes