我正在尝试使用JavaFX制作一个简单的视频播放器。我使用的是带有Maven和所有插件的IntelliJ。我使用的是Corretto SDK 18.0.2.
但是当我构建它并选择文件时,我得到这个错误:
com.sun.media.jfxmedia.MediaException: Could not create player!
I'm on Windows.
Apr 08, 2023 2:37:26 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 19 by JavaFX runtime of version 18.0.2
FilePath is- file:D:JavaNetflixProjectsrcmainjavaTestVideoTest.mp4
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml@18.0.2/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml@18.0.2/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base@18.0.2/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base@18.0.2/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@18.0.2/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@18.0.2/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@18.0.2/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base@18.0.2/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@18.0.2/javafx.scene.Node.fireEvent(Node.java:8797)
at javafx.controls@18.0.2/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls@18.0.2/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls@18.0.2/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base@18.0.2/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base@18.0.2/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base@18.0.2/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@18.0.2/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@18.0.2/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@18.0.2/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@18.0.2/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@18.0.2/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base@18.0.2/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@18.0.2/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
at javafx.graphics@18.0.2/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics@18.0.2/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
at javafx.graphics@18.0.2/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics@18.0.2/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@18.0.2/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics@18.0.2/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics@18.0.2/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics@18.0.2/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics@18.0.2/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics@18.0.2/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18.0.2/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javafx.base@18.0.2/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml@18.0.2/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml@18.0.2/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1854)
... 46 more
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media@19/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:147)
at javafx.media@19/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:519)
at javafx.media@19/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:422)
at com.example.netflixproject/Controllers.VideoPlayerController.handleButtonAction(VideoPlayerController.java:61)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 53 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media@19/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:297)
at javafx.media@19/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.media@19/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:475)
... 56 more
我做了一个文件选择器,GUI打开了,所以在我选择文件后,错误出现了,我的媒体视图中没有显示任何内容:
package Controllers;
import DAO.EpisodeDAO2;
import Utils.DataHolder;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
public class VideoPlayerController implements Initializable {
public MediaView mvVideo;
@FXML
public MediaPlayer mpVideo;
@FXML
public Media mediaVideo;
public Slider SliderTime;
public Button ButtonPPR;
public HBox HboxVolume;
public Label LabelVolume;
public Slider SliderVolume;
public Label LabelCurrentTime;
public Label LabelTotalTime;
public Label LabelSpeed;
public Label LabelFullScreen;
private boolean atEndOfVideo = false;
private boolean isPalying = false;
private boolean isMuted = false;
private ImageView ivPlay;
private ImageView ivPause;
private ImageView ivRestart;
private ImageView ivVolume;
private ImageView ivMute;
private ImageView ivFullScreen;
@FXML
public void handleButtonAction() throws Exception{
FileChooser fc= new FileChooser();
FileChooser.ExtensionFilter extensionFilter= new FileChooser.ExtensionFilter("Video files with mp4 extension only.", "*.mp4");
fc.getExtensionFilters().add(extensionFilter);
File file= fc.showOpenDialog(null);
String filePath = file.toURI().toString();
if(filePath!= null) {
System.out.println("FilePath is- "+filePath);
Media media= new Media(filePath);
mpVideo= new MediaPlayer(media);
mvVideo.setMediaPlayer(mpVideo);
mpVideo.play();
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
在使用您选择的媒体文件设置mpVideo之前正在初始化mpVideo。要解决这个问题,您可以在使用选定的文件创建MediaPlayer后移动mvVideo的初始化。
For handleButtonAction方法try this:
@FXML
public void handleButtonAction() throws Exception{
FileChooser fc= new FileChooser();
FileChooser.ExtensionFilter extensionFilter= new FileChooser.ExtensionFilter("Video files with mp4 extension only.", "*.mp4");
fc.getExtensionFilters().add(extensionFilter);
File file= fc.showOpenDialog(null);
String filePath = file.toURI().toString();
if(filePath!= null) {
System.out.println("FilePath is- "+filePath);
Media media= new Media(filePath);
mpVideo= new MediaPlayer(media);
// move initialization of mvVideo after creating the MediaPlayer
mvVideo= new MediaView(mpVideo);
mvVideo.setFitWidth(DataHolder.WIDTH);
mvVideo.setFitHeight(DataHolder.HEIGHT);
mvVideo.setPreserveRatio(false);
mvVideo.setSmooth(true);
mpVideo.setAutoPlay(true);
}
}