java播放mp4代码的简单介绍
JAVA中怎么样实现播放视频??
public class TestOne {
创新互联是一家专注于网站设计制作、成都网站建设与策划设计,都兰网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:都兰等地区。都兰做网站价格咨询:028-86922220
@SuppressWarnings("unused")
private static void Run(String filePath)
{
Runtime r = Runtime.getRuntime();
try
{
System.out.println(filePath);
r.exec("cmd /c start "+filePath);
} catch (IOException e)
{
e.printStackTrace();
System.out.println(e);
}
}
public static void main(String[] args) {
TestOne.Run("C:\橡老\Users\\whaty\\Desktop\\兰博梁搏升基尼品牌的视银岩频-YouTubeSnips.mp4");
}
}
初学了一段时间Java。想做一个应用程序,程序只需要播放一个本地视频,MP4格式,求教怎么做
使用javafx的Media,MediaPlayer,MediaView。代码如下:
import java.io.File;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import 空岁带javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.util.Duration;
public class App extends Application {
private MediaView mediaView;
private Slider processSlider;
private static final Object AUTO = new Object(), MANUAL = new Object();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("javafx视频播放器");
BorderPane root = new BorderPane();
// 菜单栏
initMenu(root);
// 视频播放控件
mediaView = initMediaView(root);
// 视频功斗芦能控件
initMediaControllView(root);
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setOnCloseRequest(event -雀姿 System.exit(0));
primaryStage.show();
}
private MenuBar initMenu(final BorderPane parent) {
Menu menu = new Menu("菜单");
MenuItem item = new MenuItem("打开");
item.setOnAction(event - {
FileChooser chooser = new FileChooser();
chooser.setTitle("选择视频文件");
chooser.getExtensionFilters().add(new ExtensionFilter("视频文件", "*.mp4", "*.flv"));
File file = chooser.showOpenDialog(parent.getScene().getWindow());
if (file != null) {
MediaPlayer player = mediaView.getMediaPlayer();
// 关闭当前的player
if (player != null) {
player.stop();
player.dispose();
}
processSlider.setValue(0);
// 创建一个新的player并自动播放
player = new MediaPlayer(new Media(file.toURI().toString()));
player.currentTimeProperty().addListener(this::mediaProcessChanged);
player.setAutoPlay(true);
mediaView.setMediaPlayer(player);
}
});
menu.getItems().add(item);
item = new MenuItem("退出");
item.setOnAction(event - parent.getScene().getWindow().hide());
menu.getItems().add(item);
MenuBar menuBar = new MenuBar(menu);
parent.setTop(menuBar);
return menuBar;
}
private MediaView initMediaView(final BorderPane parent) {
MediaView view = new MediaView();
Pane pane = new Pane(view);
parent.setCenter(pane);
// 视频宽高可随着窗口变化而自动缩放
view.fitWidthProperty().bind(Bindings.selectDouble(view.parentProperty(), "width"));
view.fitHeightProperty().bind(Bindings.selectDouble(view.parentProperty(), "height"));
view.setPreserveRatio(true);
return view;
}
private void initMediaControllView(final BorderPane parent) {
VBox bottom = new VBox(10);
bottom.setAlignment(Pos.CENTER);
parent.setBottom(bottom);
Slider slider = new Slider(0, 100, 0);
slider.valueProperty().addListener(this::processSliderChanged);
// slider.setOnMouseClicked(this::processSliderClicked);
bottom.getChildren().add(slider);
processSlider = slider;
HBox hbox = new HBox(10);
bottom.getChildren().add(hbox);
hbox.setAlignment(Pos.CENTER);
Button btn = new Button(null, new ImageView(loadImage("pause.png")));
btn.setOnAction(this::pauseOrPlay);
hbox.getChildren().add(btn);
btn = new Button(null, new ImageView(loadImage("stop.png")));
btn.setOnAction(this::stopPlay);
hbox.getChildren().add(btn);
}
private static Image loadImage(String resPath) {
return new Image(Thread.currentThread().getContextClassLoader().getResourceAsStream(resPath));
}
private void pauseOrPlay(ActionEvent event) {
MediaPlayer player = mediaView.getMediaPlayer();
if (player == null) {
return;
}
Status status = player.getStatus();
if (status == Status.READY || status == Status.PAUSED) {
player.play();
((Button) event.getTarget()).setGraphic(new ImageView(loadImage("pause.png")));
} else if (status == Status.PLAYING) {
player.pause();
((Button) event.getTarget()).setGraphic(new ImageView(loadImage("play.png")));
}
}
private void stopPlay(ActionEvent event) {
MediaPlayer player = mediaView.getMediaPlayer();
if (player == null) {
return;
}
player.stop();
}
// private void processSliderClicked(MouseEvent event) {
// Slider slider = (Slider) event.getSource();
// if (mediaView.getMediaPlayer() != null) {
// Duration d = mediaView.getMediaPlayer().getTotalDuration().multiply(slider.getValue() / slider.getMax());
// mediaView.getMediaPlayer().seek(d);
// }
// }
private void processSliderChanged(ObservableValue? extends Number observable, Number oldValue, Number newValue) {
Slider slider = (Slider) ((ReadOnlyProperty? extends Number) observable).getBean();
if (slider.getUserData() == AUTO) { // 进度条是自动改变的,因此不用设置播放器
slider.setUserData(null);
return;
}
if (mediaView.getMediaPlayer() != null) {
slider.setUserData(MANUAL);
Duration d = mediaView.getMediaPlayer().getTotalDuration().multiply(newValue.doubleValue() / slider.getMax());
mediaView.getMediaPlayer().seek(d);
}
}
private void mediaProcessChanged(ObservableValue? extends Duration observable, Duration oldValue, Duration newValue) {
if (processSlider.getUserData() == MANUAL) { // 手动点击进度条
processSlider.setUserData(null);
return;
}
MediaPlayer player = (MediaPlayer) ((ReadOnlyProperty? extends Duration) observable).getBean();
processSlider.setUserData(AUTO);
processSlider.setValue(newValue.toMillis() / player.getTotalDuration().toMillis() * 100);
}
}
如何使用Java代码获取播放文件的时长
public static void main(String[] args) {
// *.mp4,*.flv,*..3gp格式均可,其他自行测试
// vedio_path是你的视频文件路径
File source = new File("vedio_path\\Avengers2.flv");
FFMPEGLocator locator = new FFMPEGLocator() {
@Override
protected String getFFMPEGExecutablePath() {
// ffmpeg_path是你的ffmpeg.exe路径
橡派 return "ffmpeg_path轿激\\ffmpeg.exe";
}
};
Encoder encoder = new Encoder(locator);
try {
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration();
// 输出毫秒数
System.out.println(ls);
// 输出0时0分0秒0毫秒的格式
System.out.println("此视频时长为:" + ls / (60 梁帆贺* 60 * 1000) + "时" + (ls % (60 * 60 * 1000)) / 60000 + "分"
+ ((ls % (60 * 60 * 1000)) % 60000) / 1000 + "秒" + (((ls % (60 * 60 * 1000)) % 60000) % 1000)
+ "毫秒!");
} catch (Exception e) {
e.printStackTrace();
}
}
谁知道如何用纯JAVA代码播放视频文件(可以播放一种视频文件,如mp4,就可以了)
你好!简睁我们所说的mp4仅仅只是一种视频封装格式,里面的视频流或余却有衫咐滚各种编码格式!就像瓶子装水一样,“瓶子”有各种形状,如mp4 avi wmv rmvb等等…而其中装的是什么液体就不一定了!所以,看似都是.mp4文件,它的编码格式可能是H.246编码,也可能是...
文章标题:java播放mp4代码的简单介绍
浏览地址:http://hbruida.cn/article/ddpiiho.html