【问题标题】:Can't run a simple demo of a Media class无法运行 Media 类的简单演示
【发布时间】:2018-10-20 12:39:33
【问题描述】:

我在 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

【问题讨论】:

  • 你做了哪些努力?你能描述一下吗?
  • 我所做的只是复制我在视频中看到的代码,我没有添加或删除任何东西。它在视频中有效,但如果我尝试自己运行它,就会出现该错误。
  • 如果你想深入了解,你需要添加一些异常处理。
  • 尝试其他文件。
  • root.getChildren().add(root); 你不能给自己添加root

标签: java javafx


【解决方案1】:

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

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

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

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

这也有效:

Media media = new Media("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.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;

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

【讨论】:

  • 成功了,谢谢。你能解释一下我的代码出了什么问题吗?为什么它是空的?
猜你喜欢
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 2014-12-19
  • 2019-01-13
相关资源
最近更新 更多