首页 > 解决方案 > 无法运行 Media 类的简单演示

问题描述

我在 YouTube 上找到了一个关于使用Media课程的视频,但是当我尝试这样做时 - 它给了我一个错误。有人可以告诉我有什么问题吗?这是代码,正是我在视频中看到的:

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);
}

}

这是我得到的错误:

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

标签: javajavafx

解决方案


root.getChildren().add(root);您不能将根添加到自身。同样作为一个简单的打印输出可以显示,这将返回 null:

URL resource = getClass().getResource("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");

根据文档 Media构造函数需要URI格式的字符串,因此您应该使用:

URI uri = new URI("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");
Media media = new Media(uri.toString()); 

这也有效:

Media media = new Media("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");

尝试这个:

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;

public class PlayMP3 extends Application {

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

        StackPane root = new StackPane();

        URI uri = new URI("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");
        Media media = new Media(uri.toString());

        //OR Media media = new Media("http://traffic.libsyn.com/dickwall/JavaPosse373.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);
    }
}

推荐阅读