创建播放器(不播放文件) [英] Create player (which doesn't play file)

查看:102
本文介绍了创建播放器(不播放文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建视频播放器的类(代码附在下面),这个类只适用于视频文件.我想显示没有视频文件的播放器,当我发送到对象时,通过公共方法setFile",它将开始播放.

我尝试向此类发送null"而不是文件,但出现错误(java.lang.NullPointerException").

VideoPlayer player = new VideoPlayer(null, videoWidth, videoHeight);

和错误:

应用程序启动方法中的异常java.lang.reflect.InvocationTargetException在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(Unknown Source)在 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(来源不明)在 com.sun.javafx.application.LauncherImpl.launchApplication(来源不明)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(Unknown Source)在 sun.launcher.LauncherHelper$FXHelper.main(来源不明)引起:java.lang.RuntimeException:应用程序启动方法中的异常在 com.sun.javafx.application.LauncherImpl.launchApplication1(来源不明)在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(来源不明)在 com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(来源不明)在 java.lang.Thread.run(未知来源)引起:java.lang.IllegalArgumentException:uri.getScheme() == null!uri == ''在 com.sun.media.jfxmedia.locator.Locator.(来源不明)在 javafx.scene.media.Media.(来源不明)在 v04.VideoPlayer.setFile(VideoPlayer.java:90)在 v04.VideoPlayer.(VideoPlayer.java:40)在 v04.MainFrame.setVideoPlayer(MainFrame.java:218)在 v04.MainFrame.createVideoPane(MainFrame.java:246)在 v04.MainFrame.start(MainFrame.java:112)在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(来源不明)在 com.sun.javafx.application.LauncherImpl$$Lambda$54/539248128.run(来源不明)在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(来源不明)在 com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(来源不明)在 com.sun.javafx.application.PlatformImpl.lambda$null$170(来源不明)在 com.sun.javafx.application.PlatformImpl$$Lambda$49/1850153616.run(来源不明)在 java.security.AccessController.doPrivileged(Native Method)在 com.sun.javafx.application.PlatformImpl.lambda$runLater$171(来源不明)在 com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(来源不明)在 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(来源不明)在 com.sun.glass.ui.win.WinApplication._runLoop(本机方法)在 com.sun.glass.ui.win.WinApplication.lambda$null$145(来源不明)在 com.sun.glass.ui.win.WinApplication$$Lambda$37/2117255219.run(来源不明)

我也尝试在没有文件的情况下创建 Media:(同样的错误)

media = new Media(videoFile.toURI().toString());媒体播放器 = 新媒体播放器(空);mediaView = new MediaView(mediaPlayer);

班级:

import java.io.File;导入 javafx.beans.property.DoubleProperty;导入 javafx.geometry.Insets;导入 javafx.geometry.Pos;导入 javafx.scene.control.Button;导入 javafx.scene.control.Label;导入 javafx.scene.control.Slider;导入 javafx.scene.layout.BorderPane;导入 javafx.scene.layout.GridPane;导入 javafx.scene.layout.HBox;导入 javafx.scene.layout.Pane;导入 javafx.scene.layout.Region;导入 javafx.scene.layout.StackPane;导入 javafx.scene.media.Media;导入 javafx.scene.media.MediaPlayer;导入 javafx.scene.media.MediaView;导入 javafx.util.Duration;公共类 VideoPlayer 扩展了 GridPane {文件 videoFile;媒体媒体;媒体播放器媒体播放器;媒体视图媒体视图;双属性宽度;双属性高度;VideoPlayer(文件文件,双倍宽度,双倍高度){设置间隙(10);设置间隔(10);setPadding(新插入(10, 10, 10, 10));设置文件(文件);mediaView.setFitHeight(height*0.80);mediaView.setFitWidth(width*0.80);mediaView.setPreserveRatio(true);添加(mediaView, 1, 0);添加(setSlider(),1,1);}公共 HBox setSlider() {//播放和暂停按钮按钮 playButton = new Button("播放");playButton.setOnAction(e -> {if (playButton.getText().equals("Play")) {mediaPlayer.play();playButton.setText("暂停");} 别的 {mediaPlayer.pause();playButton.setText("播放");}});//倒带按钮Button rewindButton = new Button("rewind");rewindButton.setOnAction(e -> mediaPlayer.seek(Duration.ZERO));//音量滑块滑块 slVolume = new Slider();slVolume.setPrefWidth(150);slVolume.setMaxWidth(Region.USE_PREF_SIZE);slVolume.setMinWidth(30);slVolume.setValue(50);mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(100));HBox hBox = new HBox(10);hBox.setAlignment(Pos.CENTER);hBox.getChildren().addAll(playButton, rewindButton,new Label("Volume"), slVolume);返回 hBox;}公共无效集文件(文件文件){视频文件=文件;视频文件 = 文件;media = new Media(videoFile.toURI().toString());媒体播放器 = 新媒体播放器(媒体);mediaView = new MediaView(mediaPlayer);宽度 = mediaView.fitWidthProperty();高度 = mediaView.fitHeightProperty();}公共无效播放视频(){mediaPlayer.play();}}

解决方案

当您将空参数传递给构造函数的第一个 arg 时,它会被传递给 setFile,如下所示:setFile(null)>

这个功能有:

public void setFile(File file) {视频文件 = 文件;视频文件 = 文件;//你这样做了两次?这是你的错误吗?media = new Media(videoFile.toURI().toString());//这是发生空指针的地方.}

videoFile 为空,并且您正在对空对象调用 toURI().

我不知道这是否能完全解决您的问题,但除非您更改代码 - 您需要像这样防止空值:

public void setFile(File file) {如果(文件==空)返回;视频文件 = 文件;media = new Media(videoFile.toURI().toString());媒体播放器 = 新媒体播放器(媒体);mediaView = new MediaView(mediaPlayer);宽度 = mediaView.fitWidthProperty();高度 = mediaView.fitHeightProperty();}

I have a class that create video player (code is attached below), this class work only with video file. I want to show the player without a video file, and when I send to the object, by public method "setFile", it's will start to play.

I tried to send "null", instead of a file to this class, but I got an error ("java.lang.NullPointerException").

VideoPlayer player = new VideoPlayer(null, videoWidth, videoHeight);

And the error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == ''
    at com.sun.media.jfxmedia.locator.Locator.<init>(Unknown Source)
    at javafx.scene.media.Media.<init>(Unknown Source)
    at v04.VideoPlayer.setFile(VideoPlayer.java:90)
    at v04.VideoPlayer.<init>(VideoPlayer.java:40)
    at v04.MainFrame.setVideoPlayer(MainFrame.java:218)
    at v04.MainFrame.createVideoPane(MainFrame.java:246)
    at v04.MainFrame.start(MainFrame.java:112)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$54/539248128.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1850153616.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/2117255219.run(Unknown Source)

Also I tried create Media without file: (same error)

media = new Media(videoFile.toURI().toString());
mediaPlayer = new MediaPlayer(null);
mediaView = new MediaView(mediaPlayer);

The Class:

import java.io.File;

import javafx.beans.property.DoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

public class VideoPlayer extends GridPane  {

    File videoFile;
    Media media;
    MediaPlayer mediaPlayer;
    MediaView mediaView;

    DoubleProperty width;
    DoubleProperty height;

    VideoPlayer(File file, double width, double height) {

        setHgap(10);
        setVgap(10);
        setPadding(new Insets(10, 10, 10, 10));

        setFile(file);
        mediaView.setFitHeight(height*0.80);
        mediaView.setFitWidth(width*0.80);
        mediaView.setPreserveRatio(true);


        add(mediaView, 1, 0); 
        add(setSlider(),1,1);

    }

    public HBox setSlider() {

        // Play and pause button
        Button playButton = new Button("Play");
        playButton.setOnAction(e -> {
            if (playButton.getText().equals("Play")) {
                mediaPlayer.play();
                playButton.setText("pause");
            } else {
                mediaPlayer.pause();
                playButton.setText("Play");
            }
        });


        // Rewind button
        Button rewindButton = new Button("rewind");
        rewindButton.setOnAction(e -> mediaPlayer.seek(Duration.ZERO));

        // Slieder for volume
        Slider slVolume = new Slider();
        slVolume.setPrefWidth(150);
        slVolume.setMaxWidth(Region.USE_PREF_SIZE);
        slVolume.setMinWidth(30);
        slVolume.setValue(50);
        mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(100));
        HBox hBox = new HBox(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(playButton, rewindButton,
                new Label("Volume"), slVolume);
        return hBox;
    }

    public void setFile(File file) {

        videoFile=file;
        videoFile = file;
        media = new Media(videoFile.toURI().toString());
        mediaPlayer = new MediaPlayer(media);
        mediaView = new MediaView(mediaPlayer);
        width = mediaView.fitWidthProperty();
        height = mediaView.fitHeightProperty();
    }

    public void playVideo() {
        mediaPlayer.play();
    }
}

解决方案

When you pass a null argument to your constructor's first arg, this is passed to setFile as this: setFile(null)

This function has:

public void setFile(File file) {
    videoFile = file;
    videoFile = file; // You are doing this twice? Is this an error on your part?
    media = new Media(videoFile.toURI().toString()); // This is where the null pointer is happening.
}

videoFile is null, and you are calling toURI() on a null object.

I do not know if this will fully solve your issue, but unless you change your code -- you need to guard against nulls like so:

public void setFile(File file) {
    if (file == null)
        return;
    videoFile = file;
    media = new Media(videoFile.toURI().toString());
    mediaPlayer = new MediaPlayer(media);
    mediaView = new MediaView(mediaPlayer);
    width = mediaView.fitWidthProperty();
    height = mediaView.fitHeightProperty();
}

这篇关于创建播放器(不播放文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆