?? mediaplayer.java
字號:
numList.setElementAt((indexForDel+1)+"."+name,indexForDel);
}
catch(ReName e2) //自定義的異常
{
System.out.println("Exception ReName");
}
}
if(e.getSource()==info)//播放器使用說明對話框
{
dialog1.setVisible(true);
}
}
public static void main(String[] args)
{
final MediaPlayer mp=new MediaPlayer(); //初始化類
mp.setIconImage(new ImageIcon("雪景.jpg").getImage());//改變默認圖標
mp.addWindowListener(new WindowAdapter()//注冊窗口事件
{
public void windowClosing(WindowEvent e)
{
mp.exity_n();
}
}
);
System.out.println("注意:請正常關閉播放器");
}
private void openFile() //openFile 函數實現 打開音頻文件
{
fd = new FileDialog(MediaPlayer.this);
fd.setVisible(true);
if (fd.getFile() != null)
{
title = fd.getDirectory() + fd.getFile();
files=fd.getFile(); //dir =fd.getDirectory();
file=new File(title);
createPlayer();
}
}
private void openDir()//打開一個目錄,加載到播放列表中
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=fileChooser.showOpenDialog(MediaPlayer.this);
if(result==JFileChooser.CANCEL_OPTION)
return;
file=fileChooser.getSelectedFile(); //選擇文件
if(file==null||file.getName().equals(""))
JOptionPane.showMessageDialog(this,"錯誤的路徑",
"Error",JOptionPane.ERROR_MESSAGE);
String[] sFiles=file.list();//加載文件列表
for(int i=0;i<sFiles.length;i++)
{
fileName.addElement(sFiles[i]);
numList.addElement((numList.size()+1)+"."+sFiles[i]);
dirName.addElement(file.getAbsolutePath()+"\\"+sFiles[i]);
}
list.setListData(numList);
}
private void createPlayer() //新建當前播放文件
{
closePreviosPlayer();//關閉先前的媒體播放器
String extendName="該播放器不支持"+title.substring(title.lastIndexOf(".")+1)+"格式";
try
{
player=Manager.createPlayer(file.toURL());
player.addControllerListener(new ControllerHand());
player.start();
addList(files);
index=fileName.size();
list.setSelectedValue(numList.elementAt(index),true);
list.setSelectionForeground(Color.red);
setTitle(title);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,extendName,"不好意思,你弄錯了",JOptionPane.ERROR_MESSAGE);
setTitle(extendName);
}
}
private void closePreviosPlayer() //關閉先前的媒體播放器
{
if(player==null)
return;
player.stop();
player.deallocate(); //停止播放并且重新裝載DateSource
Component visual =player.getVisualComponent();
Component control=player.getControlPanelComponent();
if(visual!=null)
{
panel.remove(visual);
}
if(control!=null)
{
panel.remove(control);
}
}
private class ControllerHand implements ControllerListener//實現接口監聽事件
{
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof RealizeCompleteEvent)
{
Component visual=player.getVisualComponent();
if(visual!=null)
{
System.out.println("該播放器不支持視頻圖象功能");
setTitle("該播放器不支持視頻圖象功能");
panel.removeAll();
panel.add(visual,BorderLayout.CENTER);
}
else
{
panel.add(label,BorderLayout.CENTER);
}
Component control=player.getControlPanelComponent();
if(control!=null)
{
panel.add(control,BorderLayout.CENTER);
}
panel.doLayout();
return;
}
if (e instanceof EndOfMediaEvent)
{
if(buttonValues[0].isSelected())
{
if(fileName.size()==0)
return;
index=(int)(Math.random()*fileName.size());
}
if(buttonValues[1].isSelected())
{
if(fileName.size()==0)
return;
index=(index+1)%fileName.size();
}
if(buttonValues[2].isSelected())
{
player.setMediaTime (new Time (0));
player.start();
}
createPlayer2();
}
}
}
private void exity_n() //退出程序時的事件處理
{
int exi;
exi=JOptionPane.showConfirmDialog(this,"真的要退出嗎?","退出",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(exi==JOptionPane.YES_OPTION)
{
saveList(); //保存當前列表
System.exit(0);
}
return;
}
private void addList(String vf)//加入文件到列表中
{
try
{
fileName.addElement(vf);
numList.addElement((numList.size()+1)+"."+vf);
dirName.addElement(title);
list.setListData(numList);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
private void createPlayer2()//創建播放文件
{
try
{
title=dirName.elementAt(index).toString();
}
catch(ArrayIndexOutOfBoundsException e)
{
return;
}
file=new File(title);
closePreviosPlayer();//關閉先前的媒體播放器
String extendName="此播放器好象不支持"+title.substring(title.lastIndexOf(".")+1)+"格式";
try
{
player=Manager.createPlayer(file.toURL());
player.addControllerListener(new ControllerHand());
player.start();
list.setSelectedValue(numList.elementAt(index),true);
setTitle(title);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,extendName,"出錯了!!",JOptionPane.ERROR_MESSAGE);
String ex=null;
try
{
ex=fileName.elementAt(index).toString();
}
catch(Exception e1)
{
return;
}
fileName.removeElementAt(index);
numList.removeAllElements();
Enumeration enumFile=fileName.elements();
while(enumFile.hasMoreElements())
{
numList.addElement((numList.size()+1)+"."+enumFile.nextElement());
}
dirName.removeElementAt(index);
list.setListData(numList);
System.out.println("已經從播放列表中刪除 "+"\""+ex+"\""+" 文件,"
+"因為此播放器不支持"+ex.substring(ex.lastIndexOf(".")+1)+"格式,"
);
if(numList.size()!=0)
{
index%=numList.size();
createPlayer2();
}
}
}
private void saveList() //保存文件列表
{
Enumeration enumFile=fileName.elements();
Enumeration enumDir =dirName.elements();
try
{
output=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(listFile)));
while(enumFile.hasMoreElements())
{
listWriteFile=new ListValues(enumFile.nextElement().toString(),enumDir.nextElement().toString());
output.writeObject(listWriteFile);
}
output.flush();
output.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run() //線程起動
{
try
{
Thread.sleep(1);
}
catch(InterruptedException e)
{
}
try
{
if(!listFile.exists())
{
listFile.createNewFile();
return;
}
input=new ObjectInputStream(new BufferedInputStream(new FileInputStream(listFile)));
while(true)
{
listWriteFile=(ListValues)input.readObject();
fileName.addElement(listWriteFile.getFileName());
numList.addElement((numList.size()+1)+"."+listWriteFile.getFileName());
dirName.addElement(listWriteFile.getDirName());
}
}
catch(EOFException e)
{
try
{
input.close();//確認有元素存在并加載完畢后關閉輸入流
}
catch(IOException e1)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"文件被非正常關閉",
"非法關閉",JOptionPane.ERROR_MESSAGE);
}
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"不能創建對象","對象創建失敗",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"不能讀取文件",
" 讀取文件失敗",JOptionPane.ERROR_MESSAGE);
}
finally
{
try
{
if(input!=null)
input.close();
}
catch(IOException e)
{
}
if(dirName.isEmpty())//防止Vector越界
{
return;
}
index=(int)(Math.random()*(fileName.size()));//產生隨即數,進行隨即播放
list.setListData(numList);
createPlayer2();
}
}
private void checkMenu(MouseEvent e) //判斷checkmenu
{
if(e.isPopupTrigger())
{
indexForDel=list.locationToIndex(e.getPoint());
int[] selected={index,indexForDel};
list.setSelectedIndices(selected);
popupMenu.show(list,e.getX(),e.getY());
}
}
String reNames() throws ReName//文件該名函數
{
String name=JOptionPane.showInputDialog(this,"請輸入新的名字",fileName.elementAt(indexForDel));
if(name==null||name.equals("")) throw new ReName();
return name;
}
class ReName extends Exception//自定義異常來處理文件該名的時候發生輸入為空的情形
{
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -