Skip to content
Advertisement

Can’t run a simple demo of a Media class

I found a video on YouTube about using Media class, but when I try to do it – it gives me an error. Could somebody tell me whats wrong? Here’s the code, exactly how I saw it in the video:

package demoradio;

import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class DemoRadio extends Application {

@Override
public void start(Stage primaryStage) {
    StackPane root = new StackPane();
    try {
    URL resource = getClass().getResource("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");
    Media media = new Media(resource.toString());
    MediaPlayer player = new MediaPlayer(media);
    player.setOnError(new Runnable() {
        @Override
        public void run() {
            String err = media.getError().toString();
        }
    });

    player.setAutoPlay(true);
    } catch(RuntimeException e){}

    root.getChildren().add(root);

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

    primaryStage.setTitle("DemoRadio");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

}

And here’s the error I get:

Exception in Application start method java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Children: cycle detected: parent = StackPane@3d2c4da, node = StackPane@3d2c4da 
    at javafx.scene.Parent$2.onProposedChange(Parent.java:445)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at demoradio.DemoRadio.start(DemoRadio.java:31)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    ... 1 more 
Exception running application demoradio.DemoRadio Java Result: 1

Advertisement

Answer

root.getChildren().add(root); you can’t add root to itself. Also as a simple printout can reveal, this returns null:

URL resource = getClass().getResource("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3");

According to the documentation Media constructor expects a string in URI format, so you should use:

URI uri = new URI("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3");
Media media = new Media(uri.toString()); 

This also works:

Media media = new Media("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3");

Try this:

import java.net.URI;
import java.net.URISyntaxException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

/*
 * If you get "cannot access class com.sun.glass.utils.NativeLibLoader" exception you may need to 
 * add a VM argument: --add-modules javafx.controls,javafx.media as explained here:
 * https://stackoverflow.com/questions/53237287/module-error-when-running-javafx-media-application
 */
    
public class PlayMP3 extends Application {

    @Override
    public void start(Stage primaryStage) throws URISyntaxException {

        StackPane root = new StackPane();

        URI uri = new URI("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3");
        Media media = new Media(uri.toString());

        //OR Media media = new Media("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3");
        MediaPlayer player = new MediaPlayer(media);
        player.setOnError(new Runnable() {
            @Override
            public void run() {
                System.out.println(media.getError().toString());
            }
        });

        player.setAutoPlay(true);
        root.getChildren().add(new MediaView(player));
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("PlayMP3");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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