# 媒体播放器
## 1功能分析
写出一个媒体播放器,可以播放音频和视频,有界面,有进度条,可展示总播放时间、当前播放时间,进度条可拖动,有播放、暂停键,有快进、快退键。
## 2设计思路
### 2.1设计概述
程序由三部分组成,control用于控制视频播放,filebar用于获取视频的地址,mediacontrol用于组合各个部分,每个部分都进行了封装:
Control继承了HBox,提供了构造类初始化控制栏,提供init(Media,MediaView)方法初始化每个视频。
FileBar继承了HBox,提供了一个按钮和一个TextView,可通过get方法设置他们的事件。
MediaControl继承了BorderPane,将Control和FileBar和一个MediaView组合形成播放器,供main函数直接调用。
### 2.2界面设计
![](img/Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.001.png)
## 3设计方案
### 3.1Control类
![](img/Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.002.png)
<1>lbPlay,lbTime,lbLeft,lbRight,sdProcess,sdVolumn是控制组件
<2>duration定义了快进和快退的时长,是一个常量;currentTime是视频当前事件,endime是视频总时长。
<3>init()在视频载入时启动,用于初始化mdPlayer的属性和一些组件的状态
<3>timeStr用于将时间转化为字符串表现,显示在lbTime上
<4>Getter方法供上层类调用某些组件
主要代码如下:
```java
public Control(Media media) {
super(15);
mdPlayer = new MediaPlayer(media);
mdPlayer.stop();
mdPlayer.dispose();
//添加各种组件
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(lbPlay,lbTime,lbLeft,sdProcess,lbRight,new Label("volumn"),sdvolumn);
//暂停播放按钮事件
lbPlay.setOnMouseClicked(e->{
if (lbPlay.getText().equals("Play")){
lbPlay.setText("Pause");
mdPlayer.play();
}
else{
lbPlay.setText("Play");
mdPlayer.pause();
}
});
//快进快退按钮事件
lbRight.setOnMouseClicked(e->{
mdPlayer.seek(mdPlayer.getCurrentTime().add(duration));
});
lbLeft.setOnMouseClicked(e->{
mdPlayer.seek(mdPlayer.getCurrentTime().subtract(duration));
});
sdvolumn.setValue(50);
sdProcess.prefWidthProperty().bind(this.widthProperty().subtract(440));
}
/**
* 视频初始化
*/
public void init(Media media, MediaView mediaView) {
mdPlayer.stop();
mdPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mdPlayer);
lbPlay.setText("Play");
mdPlayer.setOnReady(() -> {
endTime = mdPlayer.getStopTime().toSeconds();
});
mdPlayer.setOnEndOfMedia(() -> {
mdPlayer.stop();
mdPlayer.seek(Duration.ZERO);
lbPlay.setText("Play");
});
mdPlayer.currentTimeProperty().addListener(ov->{
currentTime = mdPlayer.getCurrentTime().toSeconds();
lbTime.setText(timeStr(currentTime)+"/"+timeStr(endTime));
sdProcess.setValue(currentTime/endTime*100);
});
sdProcess.valueProperty().addListener(ov->{
if (sdProcess.isValueChanging()){
mdPlayer.seek(mdPlayer.getTotalDuration().multiply(sdProcess.getValue()/100));
}
});
mdPlayer.volumeProperty().bind(sdvolumn.valueProperty().divide(100));
}
/**
* 获取时间格式
* @param seconds
* @return
*/
private String timeStr(Double seconds){
Integer count = seconds.intValue();
Integer Hours = count / 3600;
count = count % 3600;
Integer Minutes = count /60;
count = count % 60;
DecimalFormat decimalFormat = new DecimalFormat("00");
String str = decimalFormat.format(Hours)+":"+decimalFormat.format(Minutes)+":"+decimalFormat.format(count);
return str;
}
```
3.2FileBar类
![](img/Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.003.png)
一个输入框和一个按钮
3.3MediaControl类
![](img/Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.004.png)
用于组合各个组件。
代码如下:
```java
public class MediaControl extends BorderPane {
private final FileBar fileBar = new FileBar();
private final MediaView mdView = new MediaView();
private File video = new File("/data/3.mp4");
private Media media = new Media(video.toURI().toString());
private final Control control = new Control(media);
public MediaControl() {
super();
this.setTop(fileBar);
this.setBottom(control);
this.setCenter(mdView);
mdView.fitWidthProperty().bind(this.widthProperty());
mdView.fitHeightProperty().bind(this.heightProperty().subtract(70));
mdView.setOnMouseClicked(e->{
if (control.getLbPlay().getText().equals("Play")){
control.getLbPlay().setText("Pause");
control.getMdPlayer().play();
}
else{
control.getLbPlay().setText("Play");
control.getMdPlayer().pause();
}
});
fileBar.getButton().setOnMouseClicked(e->{
File video = new File(fileBar.getTextField().getText());
Media media = new Media(video.toURI().toString());
control.init(media,mdView);
});
}
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
100012095-基于Java设计的媒体播放器.zip (24个子文件)
javabasicmediaplay
simplePlayer
src
simple
MediaControl.java 1KB
Control.java 4KB
main.java 472B
FileBar.java 579B
META-INF
MANIFEST.MF 47B
out
production
simplePlayer
simple
FileBar.class 1KB
MediaControl.class 3KB
Control.class 7KB
main.class 926B
META-INF
MANIFEST.MF 47B
.idea
artifacts
unnamed.xml 279B
vcs.xml 186B
misc.xml 278B
modules.xml 276B
.gitignore 39B
simplePlayer.iml 555B
simplePlayer.iml 487B
LICENSE 1KB
SimplePlayer.jar 7KB
img
Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.001.png 133KB
Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.002.png 34KB
Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.003.png 11KB
Aspose.Words.4456c934-daa7-413e-9388-8df27cea1b9d.004.png 16KB
README.md 5KB
共 24 条
- 1
资源评论
- kefuxiaohui2023-05-20资源中能够借鉴的内容很多,值得学习的地方也很多,大家一起进步!
神仙别闹
- 粉丝: 2712
- 资源: 7668
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功