实验四 windows或java平台下的Media Player(6学时)
基本要求
l 实现media player,支持多种媒体类型的播放
l 视频:avi
l 音频:wav、midi
l 图象序列:图象浏览,幻灯
l 播放列表功能
l Generic list
l 自定义的复杂播放
Bonus
界面美观-skin,可更换?
更丰富的媒体类型:
MP3,WMA,MPEG4,JPEG,PNG,WMF,Flash……
这些附加媒体类型的播放可以使用控件
这个实验参考了网上的一些相关资料,https://github.com/Al-assad
在实现这个实验的时候用到了JavaFX,需要现在eclipse中安装JavaFX插件。安装完成之后,新建一个JavaFX Project。
最后的视频播放效果如下所示,这是从网易云音乐下载的周杰伦的《七里香》MV,效果还不错,可以调节音量和进度。
音频播放效果如下所示
主要代码如下所示
package application;
public class Log {
public static void i(String tag, String content) {
System.out.println("[INFO][" + tag + "]: " + content);
}
public static void e(String tag, String content) {
System.out.println("[ERROR][" + tag + "]: " + content);
}
}
package application;
import javafx.application.Application;
import javafx.scene.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.stage.*;
import player.*;
import java.io.*;
import java.net.*;
public class Main extends Application {
MyMediaPlayer player;
FileChooser fileChooser;
MenuBar menuBar;
BorderPane pane;
Group root;
public static final String[] IMAGE_POSTFIX = {
"bmp", "dib",
"png",
"jpeg", "jpg"
};
public static final String[] AUDIO_POSTFIX = {
"mp3",
"wav"
};
public static final String[] VIDEO_POSTFIX = {
"avi",
"mp4"
};
public static final String FILENAME = "69_output_2.mp4";
public static String getPostfix(File file) {
String filename = file.getName();
String ret;
if (filename == null || filename.length() == 0) {
ret = null;
} else {
char[] curName = filename.toCharArray();
int indexDot = -1;
for (int i = curName.length - 1; i >= 0; i--) {
if (curName[i] == '.') {
indexDot = i;
break;
}
}
if (indexDot == -1) {
ret = null;
} else {
ret = filename.substring(indexDot + 1);
}
}
return ret;
}
public static boolean isThisType(String postfix, String[] postfixList) {
for (String item: postfixList) {
if (postfix.compareToIgnoreCase(item) == 0) {
return true;
}
}
return false;
}
public class UnknownFileTypeException extends Exception {
public UnknownFileTypeException() {
super("Cannot determine the file type. ");
}
public UnknownFileTypeException(String message) {
super(message);
}
}
@Override
public void start(Stage primaryStage) throws Exception{
//添加菜单栏,用于打开文件
MenuItem open = new MenuItem("Open");
Menu menuFile = new Menu("File");
menuBar = new MenuBar();
menuFile.getItems().add(open);
menuBar.getMenus().add(menuFile);
fileChooser = new FileChooser();
open.setOnAction(new EventHandler(){
public void handle(ActionEvent e){
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null){
try {
Log.i("File", file.toURI().toURL().toExternalForm());
String postfix = getPostfix(file);
if (postfix == null) {
throw new UnknownFileTypeException();
}
if (isThisType(postfix, IMAGE_POSTFIX)) {
//测试嵌入式调用
MyMediaPlayer.popup(file.toURI().toURL().toExternalForm());
} else if (isThisType(postfix, AUDIO_POSTFIX)) {
//测试嵌入式调用
MyMediaPlayer.popup(file.toURI().toURL().toExternalForm());
} else if (isThisType(postfix, VIDEO_POSTFIX)) {
/