📄 playlistframe.java
字号:
*
* @param choice
* @return File
*/
public File chooseFileOrFolder(int choice) {
JFileChooser fileChooser = new JFileChooser();
switch (choice) {
case FILE:
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
break;
case FOLDER:
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
break;
}
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return null;
File file = fileChooser.getSelectedFile();
if (file == null || file.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
return null;
} else
return file;
}
/**
* 把选取的文件加入到播放列表
*
* @param choice
*/
@SuppressWarnings("unchecked")
public void selectFileToList(int choice) {
mediaFile = chooseFileOrFolder(choice);
try {
if (mediaFile.exists()) {
switch (choice) {
case FILE:
String name = (1 + i) + "." + mediaFile.getName();
curfileIndex = i;
file.add(mediaFile);
namelist.add(name);
i++;
break;
case FOLDER:
String directory[] = mediaFile.list();
File fileName[] = mediaFile.listFiles();
for (int j = 0; j < directory.length; j++) {
if (directory[j].endsWith(".mp3")
|| directory[j].endsWith(".mpg")
|| directory[j].endsWith(".wmv")) {
String folederName = (1 + i) + "." + directory[j];
file.add(fileName[j]);
namelist.add(folederName);
i++;
}
}
break;
default:
break;
}
showPlaylist(namelist);
repaint();
}
} catch (Exception e) {
System.out.println("Error opening file!");
}
}
/**
* 得到当前要播放的文件
*
* @return File
*/
public File getCurrentFile() {
File currentFile = (File) file.get(curfileIndex);
musicList.setSelectedIndex(curfileIndex);
return currentFile;
}
/**
* 获得不同播放模式下的下一首,如果没有下一首则返回null
*
* @return File
*/
@SuppressWarnings("unchecked")
public File getNextFile() {
File nextFile = null;
if (flag && curfileIndex != cursorFileIndex) {
curfileIndex = cursorFileIndex;
nextFile = (File) file.get(curfileIndex);
} else {
switch (getModelIndex()) {
case SINGLE_PLAY:
try {
nextFile = null;
} catch (NullPointerException e) {
e.printStackTrace();
}
break;
case SINGLE_CIRCLE:
nextFile = (File) file.get(curfileIndex);
break;
case SEQ_PLAY:
if (curfileIndex < i - 1) {
curfileIndex += 1;
nextFile = (File) file.get(curfileIndex);
}
break;
case CIRCLE_PLAY:
curfileIndex = (curfileIndex + 1) % i;
nextFile = (File) file.get(curfileIndex);
break;
case RANDOM_PLAY:
Random random = new Random();
curfileIndex = random.nextInt(i);
nextFile = (File) file.get(curfileIndex);
saveFile.add(nextFile);
j1 = k;
k++;
break;
default:
break;
}
}
musicList.setSelectedIndex(curfileIndex);
setModelIndex(getModelIndex());
return nextFile;
}
/**
* 获得当前曲目的上一首
*
* @return File
*/
@SuppressWarnings("unchecked")
public File getPreFile() {
File preFile = null;
int model = getModelIndex();
switch (model) {
case SINGLE_PLAY:
break;
case SINGLE_CIRCLE:
preFile = (File) file.get(curfileIndex);
break;
case SEQ_PLAY:
if (curfileIndex >= 0) {
curfileIndex = curfileIndex - 1;
preFile = (File) file.get(curfileIndex);
}
break;
case CIRCLE_PLAY:
curfileIndex = (curfileIndex - 1 + i) % i;
preFile = (File) file.get(curfileIndex);
break;
case RANDOM_PLAY:
j1 = j1 - 1;
curfileIndex = j1;
if (j1 >= 0) {
preFile = (File) saveFile.get(curfileIndex);
} else {
Random random = new Random();
curfileIndex = random.nextInt(i - 1);
saveFile.add((File) file.get(curfileIndex));
preFile = (File) file.get(curfileIndex);
k++;
}
break;
default:
break;
}
musicList.setSelectedIndex(curfileIndex);
return preFile;
}
/**
* 设定模式
*
* @param model
*/
private void setModelIndex(int model) {
model1 = model;
}
/**
* 得到模式
*
* @return int
*/
public int getModelIndex() {
return model1;
}
/**
* 载入用户之前的播放列表
*/
@SuppressWarnings( { "unchecked", "unchecked" })
public void loadPlayList() {
URL url = getClass().getResource("support/PlayListFile");
File playListFile = new File(url.toString().substring(6));
if (playListFile.exists()) {
try {
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(playListFile));
try {
playListRecord = (PlayListRecord) input.readObject();
System.out.println("loadPlayList");
i = playListRecord.getMusicCount();
System.out.println("i=" + i);
curfileIndex = playListRecord.getCurrentFileIndex();
if (playListRecord.getMusicCount() > 0) {
Iterator fileIterator = playListRecord.getFile()
.iterator();
int j = 0;
File tempFile;
String tempName;
while (fileIterator.hasNext()) {
tempFile = (File) (fileIterator.next());
tempName = (1 + j) + "." + tempFile.getName();
file.add(tempFile);
namelist.add(tempName);
j++;
}
}
flag = playListRecord.getCursorSelected();
cursorModelMenuItem.setState(flag);
setModelIndex(playListRecord.getModel());
resetMenu(playListRecord.getModel());
if (curfileIndex >= 0)
playSelectedFile(curfileIndex);
showPlaylist(namelist);
repaint();
musicList.setSelectedIndex(curfileIndex);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("PlayList file not exist.");
}
}
/**
* 保存当前播放列表
*/
public void savePlayList() {
URL url = getClass().getResource("support/PlayListFile");
File playListFile = new File(url.toString().substring(6));
if (playListFile.exists()) {
playListRecord.setMusicCount(i);
playListRecord.setCurrentFileIndex(curfileIndex);
playListRecord.setModel(getModelIndex());
playListRecord.setFile(file);
playListRecord.setCursorSelected(flag);
try {
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream(playListFile));
output.writeObject(playListRecord);
output.flush();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Playlist file not exist.");
}
}
/**
* 模式菜单恢复默认状态
*
* @param modelIndex
*/
private void clearMenu(int modelIndex) {
// TODO Auto-generated method stub
switch (modelIndex) {
case SEQ_PLAY:
modelMenuItem[SEQ_PLAY].changIcon("Seq_not");
break;
case SINGLE_CIRCLE:
modelMenuItem[SINGLE_CIRCLE].changIcon("Sin_Cir_not");
break;
case SINGLE_PLAY:
modelMenuItem[SINGLE_PLAY].changIcon("Sin_not");
break;
case CIRCLE_PLAY:
modelMenuItem[CIRCLE_PLAY].changIcon("Cir_not");
break;
case RANDOM_PLAY:
modelMenuItem[RANDOM_PLAY].changIcon("Ran_not");
break;
default:
break;
}
}
/**
* 重新设定模式菜单
*
* @param modelIndex
*/
public void resetMenu(int modelIndex) {
// TODO Auto-generated method stub
switch (modelIndex) {
case SEQ_PLAY:
modelMenuItem[SEQ_PLAY].changIcon("Seq_is");
break;
case SINGLE_CIRCLE:
modelMenuItem[SINGLE_CIRCLE].changIcon("Sin_Cir_is");
break;
case SINGLE_PLAY:
modelMenuItem[SINGLE_PLAY].changIcon("Sin_is");
break;
case CIRCLE_PLAY:
modelMenuItem[CIRCLE_PLAY].changIcon("Cir_is");
break;
case RANDOM_PLAY:
modelMenuItem[RANDOM_PLAY].changIcon("Ran_is");
break;
default:
break;
}
}
/**
* 播放当前选中的歌曲
*
* @param curfileIndex
*/
private void playSelectedFile(int curfileIndex) {
mainFrame.stop();
mainFrame.setMediaFile((File) file.get(curfileIndex));
mainFrame.play();
}
/**
* 删除选中的文件
*/
@SuppressWarnings("unchecked")
private void deleteSelectedFile() {
// TODO Auto-generated method stub
file.add(null);
namelist.add("");
curfileIndex = cursorFileIndex;
for (int j = curfileIndex; j < i; j++) {
file.set(j, (File) file.get(j + 1));
if (j < i - 1) {
String name = (j + 1) + "." + ((File) file.get(j)).getName();
namelist.set(j, name);
}
}
namelist.remove(i);
file.remove(i);
i -= 1;
namelist.remove(i);
file.remove(i);
if (i == 0) {
curfileIndex = -1;
}
showPlaylist(namelist);
musicList.setSelectedIndex(curfileIndex);
repaint();
}
/**
* 删除重复的文件
*/
@SuppressWarnings("unchecked")
private void deleteRepeatFile() {
// TODO Auto-generated method stub
int count = 0, j, m;
for (j = 0; j < i; j++) {
for (m = j + 1; m < i; m++) {
if ((File) file.get(j) != null
&& ((File) file.get(j)).equals((File) file.get(m))) {
file.set(m, null);
++count;
}
}
}
if (count != 0) {
for (j = 0; j < i; j++) {
if ((File) file.get(j) == null) {
for (m = j + 1; m < i; m++) {
if ((File) file.get(m) != null) {
String name;
file.set(j, (File) file.get(m));
name = (j + 1) + "."
+ ((File) file.get(m)).getName();
namelist.set(j, name);
file.set(m, null);
break;
}
}
}
}
}
i -= count;
for (j = i + count - 1; j >= i; j--) {
file.remove(j);
namelist.remove(j);
}
showPlaylist(namelist);
repaint();
}
/**
* 播放列表显示
*
* @param collection
*/
@SuppressWarnings("unchecked")
private void showPlaylist(Collection collection) {
String name[] = new String[i];
int j = 0;
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
name[j] = (String) iterator.next();
j++;
}
musicList.setListData(name);
}
/**
* 复制文件
*
* @param curfileIndex
*/
private void copyFile(int curfileIndex) {
// TODO Auto-generated method stub
File copyFile = (File) file.get(curfileIndex);
customizeedit.setCopyFile(copyFile);
}
/**
* 剪切文件
*/
private void cutFile() {
// TODO Auto-generated method stub
File copyFile = (File) file.get(curfileIndex);
customizeedit.setCopyFile(copyFile);
deleteSelectedFile();
}
/**
* 粘帖文件
*/
@SuppressWarnings("unchecked")
private void pasteFile() {
// TODO Auto-generated method stub
file.add(customizeedit.getPasteFile());
String name = (1 + i) + "." + customizeedit.getPasteFile().getName();
namelist.add(name);
i++;
showPlaylist(namelist);
musicList.setSelectedIndex(i - 1);
repaint();
}
/**
* 重载的界面隐藏函数
*/
@Override
public void hideFrame() {
super.hideFrame();
mainFrame.diselectButton(2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -