2015-05-23 42 views
1

我有一个创建视频播放器的类(代码如下所示),此类只能用于视频文件。我想向玩家展示没有视频文件,当我发送到对象时,通过公共方法“setFile”,它将开始播放。我试图发送“空”,而不是文件到这个类,但我得到了一个错误(“java.lang.NullPointerException”)。我试图发送“空”,而不是一个文件到这个类,但我得到一个错误(“java.lang.NullPointerException”)。Javafx - 创建播放器(不播放文件)

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

和错误:

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) 

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

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

的类别:

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(); 
    } 
} 
+0

由于你的英语问题(其中我的意思是没有进攻),我不明白你在问什么。你能否尝试重新说一遍这样更好的理解? – Water

+0

@Water我想运行这个类没有视频文件(只显示播放器!)。这个有可能?或者给玩家一个框架... – Ramz

+0

你可以发布你的空指针栈跟踪和/或告诉我什么是有问题的行吗? – Water

回答

1

当你传递一个空参数构造函数的第一个参数,它传递给setFile就象这样:setFile(null)

此功能有:

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. 
} 

录像档案是空的,你是一个空的对象上调用toURI()

我不知道这是否会完全解决您的问题,但除非你改变你的代码 - 你需要警惕空值,像这样:

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(); 
} 
+0

谢谢,但我认为你不明白我的意思。我知道是什么导致了这个错误,我做了测试。无论如何,我通过在视频文件为空时出现的黑色矩形来解决它。再次感谢! – Ramz

+0

对不起,我不能有更多的帮助,但很高兴你修复它虽然! – Water