关键词:
我正在尝试使用javafx媒体播放视频。但是我无法在本地机器视频中播放视频。我尝试使用默认的AdvancedMedia.java文件javafx Ensemble.jar文件。我复制了相同的文件代码并粘贴在我的Eclipse中。在那里也玩了几秒钟和弯腰。几秒钟。但如果我在本地计算机上提供任何.mp4格式文件,它不会播放默认媒体文件位置,也不会出错。
我在下面放置代码
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import java.io.File;
import java.net.URI;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
/**
* An advanced media player with controls for play/pause, seek, and volume.
*
* @see javafx.scene.media.MediaPlayer
* @see javafx.scene.media.Media
*/
public class AdvancedMedia extends Application
private static final String MEDIA_URL =// "file:///D:/sri1/test.mp4";
//private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
"http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
private MediaPlayer mediaPlayer;
private AdvancedMedia.MediaControl mediaControl;
private void init(Stage primaryStage)
Group root = new Group();
primaryStage.setScene(new Scene(root));
mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
mediaPlayer.setAutoPlay(true);
mediaControl = new AdvancedMedia.MediaControl(mediaPlayer);
mediaControl.setMinSize(480,280);
mediaControl.setPrefSize(480,280);
mediaControl.setMaxSize(480,280);
root.getChildren().add(mediaControl);
public void play()
Status status = mediaPlayer.getStatus();
if (status == Status.UNKNOWN
|| status == Status.HALTED)
//System.out.println("Player is in a bad or unknown state, can't play.");
return;
if (status == Status.PAUSED
|| status == Status.STOPPED
|| status == Status.READY)
mediaPlayer.play();
@Override public void stop()
mediaPlayer.stop();
public class MediaControl extends BorderPane
private MediaPlayer mp;
private MediaView mediaView;
private final boolean repeat = false;
private boolean stopRequested = false;
private boolean atEndOfMedia = false;
private Duration duration;
private Slider timeSlider;
private Label playTime;
private Slider volumeSlider;
private HBox mediaBar;
private final Image PlayButtonImage = new Image("file:///D:/sri/UpgradeReport_Plus.gif");
ImageView imageViewPlay = new ImageView(PlayButtonImage);
//URI x=new URI("file:///D:/deepak/DEEP PHOTO.jpg");
File f=new File("file:///D:/sri/UpgradeReport_Plus.gif");
URI u=f.toURI();
private final Image PauseButtonImage = new Image(u.toString());
ImageView imageViewPause = new ImageView(PauseButtonImage);
private Pane mvPane;
private Stage newStage;
private boolean fullScreen = false;
@Override protected void layoutChildren()
if (mediaView != null && getBottom() != null)
mediaView.setFitWidth(getWidth());
mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
super.layoutChildren();
if (mediaView != null && getCenter() != null)
mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
@Override protected double computeMinWidth(double height)
return mediaBar.prefWidth(-1);
@Override protected double computeMinHeight(double width)
return 200;
@Override protected double computePrefWidth(double height)
return Math.max(mp.getMedia().getWidth(), mediaBar.prefWidth(height));
@Override protected double computePrefHeight(double width)
return mp.getMedia().getHeight() + mediaBar.prefHeight(width);
@Override protected double computeMaxWidth(double height) return Double.MAX_VALUE;
@Override protected double computeMaxHeight(double width) return Double.MAX_VALUE;
public MediaControl(final MediaPlayer mp)
this.mp=mp;
setStyle("-fx-background-color: #bfc2c7;"); // TODO: Use css file
mediaView = new MediaView(mp);
mvPane = new Pane();
mvPane.getChildren().add(mediaView);
mvPane.setStyle("-fx-background-color: black;"); // TODO: Use css file
setCenter(mvPane);
mediaBar = new HBox(5.0);
mediaBar.setPadding(new Insets(5, 10, 5, 10));
mediaBar.setAlignment(Pos.CENTER_LEFT);
BorderPane.setAlignment(mediaBar, Pos.CENTER);
final Button playButton = ButtonBuilder.create()
.minWidth(Control.USE_PREF_SIZE)
.build();
playButton.setGraphic(imageViewPlay);
playButton.setOnAction(new EventHandler<ActionEvent>()
public void handle(ActionEvent e)
updateValues();
Status status = mp.getStatus();
if (status == Status.UNKNOWN
|| status == Status.HALTED)
// don't do anything in these states
return;
if (status == Status.PAUSED
|| status == Status.READY
|| status == Status.STOPPED)
// rewind the movie if we're sitting at the end
if (atEndOfMedia)
mp.seek(mp.getStartTime());
atEndOfMedia = false;
playButton.setGraphic(imageViewPlay);
//playButton.setText(">");
updateValues();
mp.play();
playButton.setGraphic(imageViewPause);
//playButton.setText("||");
else
mp.pause();
);
mp.currentTimeProperty().addListener(new ChangeListener<Duration>()
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue)
updateValues();
);
mp.setOnPlaying(new Runnable()
public void run()
if (stopRequested)
mp.pause();
stopRequested = false;
else
playButton.setGraphic(imageViewPause);
//playButton.setText("||");
);
mp.setOnPaused(new Runnable()
public void run()
playButton.setGraphic(imageViewPlay);
//playButton.setText("||");
);
mp.setOnReady(new Runnable()
public void run()
duration = mp.getMedia().getDuration();
updateValues();
);
mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);
mp.setOnEndOfMedia(new Runnable()
public void run()
if (!repeat)
playButton.setGraphic(imageViewPlay);
//playButton.setText(">");
stopRequested = true;
atEndOfMedia = true;
);
mediaBar.getChildren().add(playButton);
// Time label
Label timeLabel = new Label("Time");
timeLabel.setMinWidth(Control.USE_PREF_SIZE);
mediaBar.getChildren().add(timeLabel);
// Time slider
timeSlider = SliderBuilder.create()
.minWidth(30)
.maxWidth(Double.MAX_VALUE)
.build();
HBox.setHgrow(timeSlider, Priority.ALWAYS);
timeSlider.valueProperty().addListener(new InvalidationListener()
public void invalidated(Observable ov)
if (timeSlider.isValueChanging())
// multiply duration by percentage calculated by slider position
if(duration!=null)
mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
updateValues();
);
mediaBar.getChildren().add(timeSlider);
// Play label
playTime = LabelBuilder.create()
//.prefWidth(130)
.minWidth(Control.USE_PREF_SIZE)
.build();
mediaBar.getChildren().add(playTime);
//Fullscreen button
Button buttonFullScreen = ButtonBuilder.create()
.text("Full Screen")
.minWidth(Control.USE_PREF_SIZE)
.build();
buttonFullScreen.setOnAction(new EventHandler<ActionEvent>()
@Override
public void handle(ActionEvent event)
if (!fullScreen)
newStage = new Stage();
newStage.fullScreenProperty().addListener(new ChangeListener<Boolean>()
@Override public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1)
onFullScreen();
);
final BorderPane borderPane = new BorderPane()
@Override protected void layoutChildren()
if (mediaView != null && getBottom() != null)
mediaView.setFitWidth(getWidth());
mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
super.layoutChildren();
if (mediaView != null)
mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
;
;
setCenter(null);
setBottom(null);
borderPane.setCenter(mvPane);
borderPane.setBottom(mediaBar);
Scene newScene = new Scene(borderPane);
newStage.setScene(newScene);
//Workaround for disposing stage when exit fullscreen
newStage.setX(-100000);
newStage.setY(-100000);
newStage.setFullScreen(true);
fullScreen = true;
newStage.show();
else
//toggle FullScreen
fullScreen = false;
newStage.setFullScreen(false);
);
mediaBar.getChildren().add(buttonFullScreen);
// Volume label
Label volumeLabel = new Label("Vol");
volumeLabel.setMinWidth(Control.USE_PREF_SIZE);
mediaBar.getChildren().add(volumeLabel);
// Volume slider
volumeSlider = SliderBuilder.create()
.prefWidth(70)
.minWidth(30)
.maxWidth(Region.USE_PREF_SIZE)
.build();
volumeSlider.valueProperty().addListener(new InvalidationListener()
public void invalidated(Observable ov)
);
volumeSlider.valueProperty().addListener(new ChangeListener<Number>()
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
if (volumeSlider.isValueChanging())
mp.setVolume(volumeSlider.getValue() / 100.0);
);
mediaBar.getChildren().add(volumeSlider);
setBottom(mediaBar);
protected void onFullScreen()
if (!newStage.isFullScreen())
fullScreen = false;
setCenter(mvPane);
setBottom(mediaBar);
Platform.runLater(new Runnable()
@Override public void run()
newStage.close();
);
protected void updateValues()
if (playTime != null && timeSlider != null && volumeSlider != null && duration != null)
Platform.runLater(new Runnable()
public void run()
Duration currentTime = mp.getCurrentTime();
playTime.setText(formatTime(currentTime, duration));
timeSlider.setDisable(duration.isUnknown());
if (!timeSlider.isDisabled() && duration.greaterThan(Duration.ZERO) && !timeSlider.isValueChanging())
timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0);
if (!volumeSlider.isValueChanging())
volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
);
private String formatTime(Duration elapsed, Duration duration)
int intElapsed = (int)Math.floor(elapsed.toSeconds());
int elapsedHours = intElapsed / (60 * 60);
if (elapsedHours > 0)
intElapsed -= elapsedHours * 60 * 60;
int elapsedMinutes = intElapsed / 60;
int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;
if (duration.greaterThan(Duration.ZERO))
int intDuration = (int)Math.floor(duration.toSeconds());
int durationHours = intDuration / (60 * 60);
if (durationHours > 0)
intDuration -= durationHours * 60 * 60;
int durationMinutes = intDuration / 60;
int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;
if (durationHours > 0)
return String.format("%d:%02d:%02d/%d:%02d:%02d",
elapsedHours, elapsedMinutes, elapsedSeconds,
durationHours, durationMinutes, durationSeconds);
else
return String.format("%02d:%02d/%02d:%02d",
elapsedMinutes, elapsedSeconds,
durationMinutes, durationSeconds);
else
if (elapsedHours > 0)
return String.format("%d:%02d:%02d",
elapsedHours, elapsedMinutes, elapsedSeconds);
else
return String.format("%02d:%02d",
elapsedMinutes, elapsedSeconds);
@Override public void start(Stage primaryStage) throws Exception
init(primaryStage);
primaryStage.show();
play();
public static void main(String[] args) launch(args);
import java.io.File;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Video extends Application
private String Dir = System.getProperty("user.dir");
public static void main(String[] args) throws Exception
launch(args);
@Override
public void start(Stage stage) throws Exception
//goes to user Directory
File f = new File(Dir, "Epic Lightsaber Duel - Star Wars_ The Force Awakens.mp4");
//Converts media to string URL
Media media = new Media(f.toURI().toURL().toString());
javafx.scene.media.MediaPlayer player = new javafx.scene.media.MediaPlayer(media);
MediaView viewer = new MediaView(player);
//change width and height to fit video
DoubleProperty width = viewer.fitWidthProperty();
DoubleProperty height = viewer.fitHeightProperty();
width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
viewer.setPreserveRatio(true);
StackPane root = new StackPane();
root.getChildren().add(viewer);
//set the Scene
Scene scenes = new Scene(root, 500, 500, Color.BLACK);
stage.setScene(scenes);
stage.setTitle("Riddle Game");
stage.setFullScreen(true);
stage.show();
player.play();
这对我来说非常适合
要播放本地视频使用以下
private static File file = new File("d:/videos/xyz.mp4");
private static final String MEDIA_URL = file.toURI().toString();
这对我有用。希望这会帮助你。
多媒体文件格式:mp4格式(代码片段)
...使用系统默认的播放器即可以播放。MP4格式是最常见的多媒体文件格式。一、MP4格式标准介绍MP4格式标准为ISO-14496Part12、ISO-14496Part14,标准内容不是很多,下面我们来介绍一下格式标准中一些重要的信息。MP4是一种描述较为全面... 查看详情
mp4的定义是啥?怎么区别mp4和视频mp3?
...人视频播放器)也可以叫做PMP(PortableMediaPlayer,便携式媒体播放器)。现在对MP4播放器的功能没有具体界定,虽然不少厂商都将它定义为多媒体影音播放器,但它除了看电影的基本功能外还支持音乐播放、浏览图片,甚至部分... 查看详情
mp4的视频打开可以播放在页面上无法打开
MP4视频在本地可以播放但是使用html5的video多媒体播放标签不能正常播放只有一个进度条而不显示图像?,其实就是一个视频编码的问题,格式虽然都是MP4但是html中只支持H.264的编码格式,转码时只有选择关于<video>标签所支... 查看详情
chrome或者ie浏览器支持h264视频流播放吗?(不是mp4文件,仅仅是h264编码的视频流)
...VideoEncode为MPEG4(DivX).修改成AVC(H264)之后,问题解决:现在可以成功上传视频了:参考技术A支持h264视频播放追问可以给出代码吗?比如接收到一个Buffer就是h264编码的视频流,怎么播放呢? 参考技术B可以的你试试就知道啦追问请... 查看详情
mp4是啥格式文件?
参考技术AMP4视频格式有1:AVIAVI是一种电脑上比较常见的媒体文件格式,英文全名为AudioVideoInterleaved。通常情况下,AVI文件可以包含多个类型的媒体流,最典型的情况是一个音频流和一个视频流。AVI格式包含多种编码格式,比如D... 查看详情
ffmpeg音视频解码
1.播放多媒体文件步骤通常情况下,我们下载的视频文件如MP4,MKV、FLV等都属于封装格式,就是把音视频数据按照相应的规范,打包成一个文本文件。我们可以使用MediaInfo这个工具查看媒体文件的相关信息。所以当我们播放一个... 查看详情
mp4只能保存一个书签吗
不,MP4文件可以保存多个书签。书签可以帮助您快速访问您最喜欢的视频或音频文件中的特定位置。您可以在MP4文件中添加书签,以便更轻松地访问您最喜欢的视频或音频文件的特定位置。书签可以帮助您快速定位到您最喜欢的... 查看详情
安卓liveplayer是干嘛的
...速度等。参考技术A安卓LivePlayer是一款面向安卓系统的流媒体播放器,它可以播放来自互联网的多种流媒体内容,如视频、音乐、电视节目等。它使用起来简单高效,支持多种流媒体播放格式,可以播放流媒体数据,还支持视频... 查看详情
mp4文件格式怎么在电脑里播放?
第一有可能视频本身损坏,第二可以尝试下面方法。步骤如下:首先需要下载一个软件叫做格式工厂。2.打开格式工厂,可以看到左边有很多不同类型的格式,选择你想转换的一种即可。3.选好后点击“添加文件”,就可以选择视... 查看详情
moviepy输出视频mp4文件windows媒体播放器播放无画面只有声音问题的解决办法
一、问题这2天用Moviepy合成了一个视频文件,用windows媒体播放器播放时只有声音没有画面,如图:而用手机播放器却可以正常播放。二、问题定位文件基本信息如下:再看视频相关信息:可以看到Windows中文件信息中没有视频的... 查看详情
moviepy输出视频mp4文件windows媒体播放器播放无画面只有声音问题的解决办法
一、问题这2天用Moviepy合成了一个视频文件,用windows媒体播放器播放时只有声音没有画面,如图:而用手机播放器却可以正常播放。二、问题定位文件基本信息如下:再看视频相关信息:可以看到Windows中文件信息中没有视频的... 查看详情
音视频开发系列——全面了解androidmediaextractor(代码片段)
MediaExtractor的作用及使用场景MediaExtractor是Android系统中的媒体解封装库,它可以从视频或音频文件中提取媒体轨道(如视频、音频、字幕等)的数据,主要作用是将多媒体文件中的音频、视频等数据分离出来,... 查看详情
m3u8格式的视频文件怎么转成mp4在电脑上播放?
...的音频文件下载下来再作处理。m3u格式的文件只是存储多媒体播放列表,提供了一个指向其他位置的音频视频文件的索引,你播放的还是那些被指向的文件,用记事本打开m3u文件可以查看所指向文件的地址及文件的属性,以选用... 查看详情
怎样可以让windowsmediaplayer可以播放mp4格式电影
参考技术Awindowsmediaplayer可以播放mp4视频文件,但要求视频编码方式必须是H264。同为MP4格式的视频文件,它的编码方式有多种:DIVX、XVID、H264等。而windowsmediaplayer播放器,可以正常播放以H264编码的MP4格式。但对以DIVX、XVID编码的M... 查看详情
windowsmediaplayer如何播放mp4视频文件?
...的时候,暴风影音自动下载了一个mp4-4hk的解码器之后就可以播放了。现在我需要用windowsmediaplayer来播放这个视频,请问我需要下载什么解码器才可以呢,最好能给出下载地址,谢谢~~1、首先在PC下载网搜索下载windowsmediaplayer软件... 查看详情
m3u8是啥格式,能在手机上播放吗?
...或者是”添加文件夹“,将m3u8的视频文件添加进入,也可以使用拖拽文件的方式将文件拖拽到软件中;步骤3:调整参数选择输出格式视频中的MP4格式,输出目录也可以根据自己的需求自定义一下;步骤4:完成转换视频的相关参... 查看详情
html5可以播放哪些格式的视频
...频文件而赋予视频文件的一种识别符号。2、3GP是一种3G流媒体的视频编码格式,主要是为了配合3G网络的高传输速度而开发的,也是目前手机中最为常见的一种视频格式。3、MPEG(运动图像专家组)是MotionPictureExpertsGroup的缩写,... 查看详情
部署简单的流媒体播放网页ffmpeg的安装和使用(代码片段)
...换器ffplay.exe:简单的音视频播放器ffserver.exe:流媒体服务器ffprobe.exe:简单的多媒体码流分析器解压缩,并把bin目录配置到环境变量中,执行ffmpeg-version查看是否看装成功ffmpeg的使用ffmpeg\\[全局参数]\\[输入文... 查看详情