public class TestOne {
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),都蘭網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:都蘭等地區(qū)。都蘭做網(wǎng)站價(jià)格咨詢: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");
}
}
使用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();
//?關(guān)閉當(dāng)前的player
if?(player?!=?null)?{
player.stop();
player.dispose();
}
processSlider.setValue(0);
//?創(chuàng)建一個(gè)新的player并自動(dòng)播放
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);
//?視頻寬高可隨著窗口變化而自動(dòng)縮放
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)?{?//?進(jìn)度條是自動(dòng)改變的,因此不用設(shè)置播放器
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)?{?//?手動(dòng)點(diǎn)擊進(jìn)度條
processSlider.setUserData(null);
return;
}
MediaPlayer?player?=?(MediaPlayer)?((ReadOnlyProperty??extends?Duration)?observable).getBean();
processSlider.setUserData(AUTO);
processSlider.setValue(newValue.toMillis()?/?player.getTotalDuration().toMillis()?*?100);
}
}
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();????
//?輸出毫秒數(shù)????
System.out.println(ls);????
//?輸出0時(shí)0分0秒0毫秒的格式????
System.out.println("此視頻時(shí)長為:"?+?ls?/?(60?梁帆賀*?60?*?1000)?+?"時(shí)"?+?(ls?%?(60?*?60?*?1000))?/?60000?+?"分"????
+?((ls?%?(60?*?60?*?1000))?%?60000)?/?1000?+?"秒"?+?(((ls?%?(60?*?60?*?1000))?%?60000)?%?1000)????
+?"毫秒!");????
}?catch?(Exception?e)?{????
e.printStackTrace();????
}????
}
你好!簡睜我們所說的mp4僅僅只是一種視頻封裝格式,里面的視頻流或余卻有衫咐滾各種編碼格式!就像瓶子裝水一樣,“瓶子”有各種形狀,如mp4 avi wmv rmvb等等…而其中裝的是什么液體就不一定了!所以,看似都是.mp4文件,它的編碼格式可能是H.246編碼,也可能是...
名稱欄目:java播放mp4代碼的簡單介紹
網(wǎng)頁地址:http://jinyejixie.com/article40/ddpiiho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、云服務(wù)器、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)