Kann nicht eine einfache Demo einer Medienklasse ausgeführt

stimmen
0

Ich fand ein Video auf YouTube über die Verwendung von MediaKlasse, aber wenn ich versuche , es zu tun - es gibt mir einen Fehler. Könnte mir jemand sagen , was ist falsch? Hier ist der Code, genau , wie ich es in dem Video sah:

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

}

Und hier ist der Fehler, den ich erhalten:

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
Veröffentlicht am 20/10/2018 um 12:39
quelle vom benutzer
In anderen Sprachen...                            


1 antworten

stimmen
0

root.getChildren().add(root);Sie können nicht root selbst hinzufügen. Auch als ein einfacher Ausdruck offenbaren kann, das den Wert null zurück:

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

Gemäß der Dokumentation Media Konstruktor erwartet Zeichenfolge im URI - Format, so sollten Sie verwenden:

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

Dies funktioniert auch:

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

Versuche dies:

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);
    }
}
Beantwortet am 20/10/2018 um 13:40
quelle vom benutzer

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more