JavaFX - Playing loop video

白昼怎懂夜的黑 提交于 2019-12-24 14:19:13

问题


How should I loop a video in JavaFX? I'm trying to just play a video one time after another, so I was looking for some sample code in many places and I could'nt make it work!

This is what doesn't work for me:

public MyMediaPlayer (){
    media = new Media(getVideo());
    mediaPlayer = new MediaPlayer(media);
    mediaView = new MediaView(mediaPlayer);
    startMediaPlayer();
}

private String getVideo() {
    return getClass().getResource("videos/limbo.mp4").toString();
}

public final void startMediaPlayer() {
    mediaPlayer.setMute(true);
    mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE); //this is the line that should do the magic, but it doesn't...
    mediaPlayer.play();
}

回答1:


The following works for me (video loops forever). I can't replicate your issue.

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;

public class VideoPlayerExample extends Application {
  public static void main(String[] args) throws Exception { launch(args); }
  @Override public void start(final Stage stage) throws Exception {
    final MediaPlayer oracleVid = new MediaPlayer(
      new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv")
    );
    stage.setScene(new Scene(new Group(new MediaView(oracleVid)), 540, 208));
    stage.show();

    oracleVid.setMute(true);
    oracleVid.setRate(20);

    oracleVid.setCycleCount(MediaPlayer.INDEFINITE);

    oracleVid.play();
  }
}

I'm under Java 7, doesn't work there . . . the problem seems to be MP4 format.

If you can't play MP4 files, either:

  1. The MP4 is not encoded in a format JavaFX understands (the JavaFX 2.2 Media javadoc details the allowed formats).

    OR

  2. You don't have appropriate codecs installed on your machine to allow the MP4 file to be decoded. See the JavaFX 2.2 Media system requirements for information on what you need to install on your machine to allow MP4 files to be displayed.



来源:https://stackoverflow.com/questions/19597460/javafx-playing-loop-video

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!