我正在尝试播放有关Java FX组件的视频但无法播放。 我知道如何使用vlcj播放视频,但当我尝试相同时我无法在javafx中播放
@Override
public void start(Stage primaryStage)
{
File f = new File("Video.mp4");
new NativeDiscovery().discover();
EmbeddedMediaPlayerComponent playerComponent = new EmbeddedMediaPlayerComponent();
/*I dont know where to add the playerComponent as we do in swing*/
MediaPlayer mp=playerComponent.getMediaPlayer();
StackPane root = new StackPane();
Scene scene = new Scene(root, 700, 700);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
//above code is for javafx where I am getting a problem
//below code for swing
public void playVideo()
{
new NativeDiscovery().discover();
EmbeddedMediaPlayerComponent playerComponent = new EmbeddedMediaPlayerComponent();
jif[i].setContentPane(playerComponent);
MediaPlayer p = playerComponent.getMediaPlayer();
ft = new FileTest();
videoList = ft.getVideoList();
jif[i].setVisible(true);
frame.setVisible(true);
p.setAspectRatio(width[i] + ":" + height[i]);
p.playMedia(videoList[0]);
}
我想知道我需要在javafx中为我们在swing中做的settingContentPane做些什么。
答案 0 :(得分:2)
我有同样的问题。然后,我如上所述尝试了vlcj-javafx
,但是采用该解决方案后,我遇到了性能问题,无法观看视频。
因此,我尝试使用具有更好性能的awt。
最后我得到了一个AWT JWindows,该窗口在JavaFx Window聚焦时位于最顶部,并像javafx Window一样改变位置。 我知道这不是一个很好的解决方案,但是它比我以前使用java fx尝试过的任何解决方案都要好
我没有Java开发经验,也不熟悉Java Fx。但是它运作的很好。 代码说明: 您可以在Java Fx中使用的该面板awt面板将被绘制在该面板上。 要播放视频,请使用:
VlcPlayerPane#play(video_file_path.mp4)
MyMediaPlayerEventListener
是MediaPlayerEventListener
,但每个方法都有一个空的默认值。
//some imports here....
public class VlcPlayerPane extends Pane {
static {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC");
}
private JWindow frame;
private Timer videoMoveTimer;
private EmbeddedMediaPlayerComponent mediaPlayerComponent;
private Runnable trackFinishedAction = null;
private Consumer<Long> onTimeChanged = null;
private Consumer<Float> onPositionChanged = null;
private Consumer<Long> onMaxLenghtChanged = null;
public VlcPlayerPane() {
this.setBackground(
new Background(new BackgroundFill(Color.BLACK, new CornerRadii(2), new Insets(2))));
Platform.runLater(() ->
getScene().getWindow().focusedProperty().addListener((a, b, c) -> {
if (frame != null) {
if (c) {
frame.setAlwaysOnTop(true);
} else {
frame.setAlwaysOnTop(false);
}
}
}));
}
private void createMoveTimer() {
videoMoveTimer = new Timer();
videoMoveTimer.schedule(new TimerTask() {
private Bounds oldBounds = null;
@Override
public void run() {
Bounds bounds = localToScreen(getBoundsInLocal());
if (!((Stage) getScene().getWindow()).isIconified()) {
if (!frame.isVisible()) {
frame.setVisible(true);
}
if (oldBounds == null ||
bounds.getMinX() != oldBounds.getMinX() ||
bounds.getMinY() != oldBounds.getMinY() ||
bounds.getWidth() != oldBounds.getWidth() ||
bounds.getHeight() != oldBounds.getHeight()) {
oldBounds = bounds;
frame.setLocation(((int) bounds.getMinX()), ((int) bounds.getMinY()));
frame.setSize(((int) bounds.getWidth()), ((int) bounds.getHeight()));
}
} else {
if (frame.isVisible()) {
frame.setVisible(false);
oldBounds = null;
frame.setSize(0, 0);
}
}
}
}, 0, 50);
}
private void createPlayer() {
frame = new JWindow();
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
frame.setContentPane(mediaPlayerComponent);
frame.setSize(0, 0);
frame.setVisible(true);
createMoveTimer();
mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MyMediaPlayerEventListener() {
@Override
public void finished(MediaPlayer mediaPlayer) {
if (trackFinishedAction != null) {
trackFinishedAction.run();
}
}
@Override
public void timeChanged(MediaPlayer mediaPlayer, long newTime) {
if (onTimeChanged != null) {
onTimeChanged.accept(newTime);
}
}
@Override
public void positionChanged(MediaPlayer mediaPlayer, float newPosition) {
if (onPositionChanged != null) {
onPositionChanged.accept(newPosition);
}
}
@Override
public void lengthChanged(MediaPlayer mediaPlayer, long newLength) {
if(onMaxLenghtChanged !=null){
onMaxLenghtChanged.accept(newLength);
}
}
});
}
public void play(String path) {
if (mediaPlayerComponent == null) {
createPlayer();
}
try {
mediaPlayerComponent.getMediaPlayer().playMedia(path);
}catch (Exception e){
e.printStackTrace();
}
}
public void pause() {
if(mediaPlayerComponent != null) {
mediaPlayerComponent.getMediaPlayer().pause();
}
}
public void continuePlaying() {
if(mediaPlayerComponent != null) {
mediaPlayerComponent.getMediaPlayer().play();
}
}
public void stopVideo() {
if (mediaPlayerComponent != null &&
mediaPlayerComponent.getMediaPlayer() != null) {
mediaPlayerComponent.getMediaPlayer().stop();
}
videoMoveTimer.cancel();
frame.setVisible(false);
frame.dispose();
frame = null;
mediaPlayerComponent = null;
videoMoveTimer = null;
}
public void setPlayPosition(int seconds){
if(mediaPlayerComponent != null){
mediaPlayerComponent.getMediaPlayer().setTime(seconds*1000);
}
}
public void registerOnTimeChanged(Consumer<Long> action) {
onTimeChanged = action;
}
public void registerOnPositionChanged(Consumer<Float> action) {
onPositionChanged = action;
}
public void registerOnFinishedTrack(Runnable action) {
trackFinishedAction = action;
}
public void registerOnMaxLengthChanged(Consumer<Long> action){
onMaxLenghtChanged = action;
}
public long getLength() {
if(mediaPlayerComponent == null){
return 0;
}
return mediaPlayerComponent.getMediaPlayer().getLength();
}
public float getActualPositionPercent() {
if(mediaPlayerComponent == null){
return 0;
}
return mediaPlayerComponent.getMediaPlayer().getPosition();
}
public long getActualPositionSecond() {
if(mediaPlayerComponent == null){
return 0;
}
return mediaPlayerComponent.getMediaPlayer().getTime();
}