📄 mainplayer.java
字号:
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
});
//滚轴控制音量相关操作
addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
soundControl.change(0 - e.getWheelRotation());
}
});
setSize(275, 116);
//定位到屏幕正中
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setLocation((width - getWidth()) / 2, (height - getHeight()) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
playListFrame = new PlayListFrame(this);
playListFrame.setVisible(configRecord.isShowPlayListFrame());
}
/**
* 创建JMF播放器
*/
public void creatPlayer() {
if (mediaFile == null)
return;
String filePath = "file:/" + mediaFile.getAbsolutePath();
MediaLocator mrl = new MediaLocator(filePath);
mediaInfoLabel.setMediaInfo(mediaFile);
if (player != null) {
stop();
}
try {
player = Manager.createPlayer(mrl);
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.addControllerListener(this);
}
/**
* 播放器执行播放操作
*/
public void play() {
if (mediaFile == null)
return;
if (player == null) {
creatPlayer();
}
if (player != null) {
player.prefetch();
player.start();
playButton.setVisible(false);
pauseButton.setVisible(true);
playerStateLabel.setText("状态:播放");
timeControl.start();
}
}
/**
* 播放器执行暂停操作
*/
public void pause() {
if (player == null)
return;
player.stop();
playButton.setVisible(true);
pauseButton.setVisible(false);
playerStateLabel.setText("状态:暂停");
timeControl.stop();
}
/**
* 播放器执行停止操作
*/
public void stop() {
setTimeLabel("00:00");
if (player != null) {
player.close();
player = null;
}
playButton.setVisible(true);
pauseButton.setVisible(false);
playerStateLabel.setText("状态:停止");
timeControl.setProcessPercent(0);
timeControl.stop();
}
/**
* 定位媒体时间
*/
public void setTime() {
if (player == null) {
return;
}
long dura = player.getDuration().getNanoseconds();
if (dura < 0 || dura > 3 * 3600 * 1000000000L)
return;
long nano = (long) (dura * timeControl.getProcessPercent());
if (nano < 0)
nano = 0;
if (nano > dura)
nano = dura;
player.setMediaTime(new Time(nano));
if (player.getTargetState() < Player.Started)
player.prefetch();
}
/**
* 设置音量
*/
public void setGain() {
gainLevel = (float) soundControl.getProcessPercent();
if (gainControl != null) {
if (soundDisabled) {
gainControl.setLevel(0);
} else {
gainControl.setLevel(gainLevel);
}
}
}
/**
* 播放器状态响应函数
*/
@Override
public synchronized void controllerUpdate(ControllerEvent event) {
if (player == null)
return;
if (event instanceof RealizeCompleteEvent) {
gainControl = (GainControl) player
.getControl("javax.media.GainControl");
setGain();
} else if (event instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
playNext();
setTimeLabel("00:00");
}
}
/**
* 最小化全部界面
*/
public void minimum() {
setExtendedState(ICONIFIED);
playListFrame.setExtendedState(ICONIFIED);
}
/**
* 选择媒体文件并播放
*/
public void selectFileToPlay() {
playListFrame.selectFileToList(0);
mediaFile = playListFrame.getCurrentFile();
stop();
play();
}
/**
* 切换播放模式完整或迷你
*/
public void changeDisplayModel() {
// TODO
}
/**
* 处理关闭时有关操作
*/
public void close() {
saveConfig();
playListFrame.savePlayList();
if (player != null) {
player.close();
}
System.exit(0);
}
/**
* 播放顺序上一首
*/
public void playPre() {
mediaFile = playListFrame.getPreFile();
stop();
play();
}
/**
* 播放下一首.
* @param playModel
*/
public void playNext() {
mediaFile = playListFrame.getNextFile();
stop();
play();
}
/**
* 显示或隐藏播放列表窗口.
* @param showPlayListFrame
*/
public void showPlayListFrame(boolean showPlayListFrame) {
if (playListFrame == null)
return;
playListFrame.setVisible(showPlayListFrame);
}
/**
* 载入用户设置文件.
*/
public void loadConfig() {
URL url = getClass().getResource("support/ConfigFile");
File configFile = new File(url.toString().substring(6));
if (configFile.exists()) {
try {
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(configFile));
try {
configRecord = (ConfigRecord) input.readObject();
mediaFile = configRecord.getMediaFile();
gainLevel = configRecord.getGainLevel();
soundDisabled = configRecord.isSoundDisabled();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
configRecord = new ConfigRecord();
e.printStackTrace();
}
} else {
System.out.println("Config file not exist.");
}
}
/**
* 保存用户设置.
*/
public void saveConfig() {
URL url = getClass().getResource("support/ConfigFile");
File configFile = new File(url.toString().substring(6));
if (configFile.exists()) {
configRecord.setMeidaFile(mediaFile);
configRecord.setGainLevel(gainLevel);
configRecord.setShowPlayListFrame(playListFrame.isVisible());
configRecord.setSoundDisabled(soundDisabled);
try {
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream(configFile));
output.writeObject(configRecord);
output.flush();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Config file not exist.");
}
}
/**
* 设置声音标签.
* @param soundPercent
*/
public void setSoundLabel(String soundPercent) {
if (soundDisabled) {
soundStateLabel.setText("静音");
} else {
if (soundPercent == "")
soundStateLabel.setText("立体声");
else
soundStateLabel.setText(soundPercent);
}
}
/**
* 设置时间标签.
* @param timePercent
*/
public void setTimeLabel(String timePercent) {
if (timePercent == "") {
int totleSecond = (int) getMediaTime().getSeconds();
timeLabel.setText(String.format("%1$02d:%2$02d", totleSecond / 60,
totleSecond % 60));
} else
timeLabel.setText(timePercent);
}
//TODO
/**
* 获取帧率.
* @return int
*/
public int getDuration() {
if (player != null) {
return (int) Math.random() * 10;
} else
return 0;
}
/**
* 获得纳秒表示的媒体文件总长度.
* @return long
*/
public long getTotaNanoseconds() {
if (player == null)
return -1;
return player.getDuration().getNanoseconds();
}
/**
* 获得纳秒表示的播放时间长度.
* @return long
*/
public long getCurrentNanoseconds() {
if (player == null)
return -1;
return player.getMediaNanoseconds();
}
/**
* 获得已经播放的时间长度.
* @return Time
*/
public Time getMediaTime() {
if (player == null)
return new Time(0);
return player.getMediaTime();
}
/**
* 设置将播放的媒体文件.
* @param mediaFile
*/
public void setMediaFile(File mediaFile) {
this.mediaFile = mediaFile;
}
/**
* 获取当前播放器状态
* @return
*/
public int getPlayerStates() {
if (player == null)
return 0;
else
return 1;
}
/**
* 取消对应双态按钮的选定状态
* @param toogleButton
*/
public void diselectButton(int i) {
switch (i) {
case 0:
lyricButton.setSelected(false);
break;
case 1:
videoButton.setSelected(false);
break;
case 2:
playListButton.setSelected(false);
break;
default:
break;
}
}
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
MainPlayer mainForm = new MainPlayer();
mainForm.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -