java mediaplayer 循环,JavaFX - 播放循环视频

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();

}

解决方案

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:

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

OR

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值