?? mediaplayer.java
字號:
private void createPlayer()
{
closePreviosPlayer();//關(guān)閉先前的媒體播放器
String extendName="此播放器不支持"+title.substring(title.lastIndexOf(".")+1)+"格式";
try
{
player=Manager.createPlayer(file.toURL());//javax.media.Manager直接繼承于java.lang.object,且它為final,不能被繼承
player.addControllerListener(new ControllerHand());
player.start();
addList(files);
index=fileName.size()-1;
list.setSelectedValue(numList.elementAt(index),true);
setTitle(title);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,extendName,"出錯了!!",JOptionPane.ERROR_MESSAGE);
setTitle(extendName);
}
}
private void closePreviosPlayer()//把先前打開的播放器關(guān)閉
{
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)//如果圖象存在則去掉以前的圖象加載當前圖象
{
panel.removeAll();
panel.add(visual,BorderLayout.CENTER);
}
Component control=player.getControlPanelComponent();//是一個操作時間軸的可視組件(包括開始,停止,回放),
//也包含了一些媒體流的有用信息
if(control!=null)
{
panel.add(control,BorderLayout.SOUTH);
}
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())//進行順序循環(huán)播放
{
if(fileName.size()==0)
return;//必須有此if語句,否則當用戶把播放列表清空的時候發(fā)生異常,偶然的機會發(fā)現(xiàn)的
//現(xiàn)在感覺測試軟件真是太重要了,看來以后程序做好后要反復測試,考慮各種情況
index=(index+1)%fileName.size();
}
if(buttonValues[2].isSelected())//進行單曲播放
{
player.setMediaTime (new Time (0)); //將時間返回開始的0時刻然后播放
player.start();
}
createPlayer2();
}
}
}
private void exity_n()//退出操作,先保存列表,然后退出
{
saveList();
System.exit(0);
}
//清單改變一次就重新載入一次
private void addList(String vf)//像播放清單中增加選項
{
fileName.addElement(vf);
numList.addElement((numList.size()+1)+"."+vf);
dirName.addElement(title);
list.setListData(numList);
}
private void createPlayer2()//創(chuàng)建一個player對象并使其播放
//當雙擊鼠標時會播放鼠標擊中的歌曲,并且不會出現(xiàn)能播放的格式也播放不了,在刷新文件后(即雙擊鼠標時更換播放曲目時)使用
{
try{
title=dirName.elementAt(index).toString();//將當前目錄名設(shè)為title
}
catch(ArrayIndexOutOfBoundsException e)//如果數(shù)組越界則返回
{return;}
file=new File(title);//以title為名創(chuàng)建一個file實例,下面將file轉(zhuǎn)換為URL用來創(chuàng)建player對象
closePreviosPlayer();//關(guān)閉先前的媒體播放器
String extendName="此播放器不支持"+title.substring(title.lastIndexOf(".")+1)+"格式";
try
{
player=Manager.createPlayer(file.toURL());//javax.media.Manager直接繼承于java.lang.object,且它為final,不能被繼承 //Manager用來創(chuàng)建Player對象
player.addControllerListener(new ControllerHand());
player.start();//設(shè)置為開始播放
list.setSelectedValue(numList.elementAt(index),true);//將當前所在 的位置在list中置為選中項
setTitle(title); //名稱為當前目錄名稱
}
catch(Exception e)//若出現(xiàn)不能播放的格式,則將該文件從播放列表中刪除,并且給出相應的提示信息
{
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);
JOptionPane.showMessageDialog(this,"已經(jīng)從播放列表中刪除 "+"\""+ex+"\""+" 文件,"
+"因為此播放器不支持"+ex.substring(ex.lastIndexOf(".")+1)+"格式","出錯了!!!",JOptionPane.ERROR_MESSAGE);
}
}
private void saveList()//將播放列表保存到list中使下次執(zhí)行是在上一次執(zhí)行的基礎(chǔ)上
{//Enumeration接口在包java.util 中
//它生成一系列元素,一次生成一個,
//連續(xù)調(diào)用nextElement方法將返回一系列的連續(xù)元素
Enumeration enumFile=fileName.elements();
Enumeration enumDir =dirName.elements();
try
{
output=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(listFile)));
while(enumFile.hasMoreElements())//如果enumFile中有元素則寫入文件中
{//將文件名與目錄都保存在ListValues對象中最終實現(xiàn)保存在文件中
listWriteFile=new ListValues(enumFile.nextElement().toString(),enumDir.nextElement().toString());
output.writeObject(listWriteFile);//將對象寫入文件中
}
output.flush();
output.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()//其中實現(xiàn)了將選擇的文件加入到播放列表中
//并進行隨即播放,即只要執(zhí)行該文件(播放列表不空)則就會有歌曲播放
{
try
{
Thread.sleep(1);
}
catch(InterruptedException e)
{
}
try
{
if(!listFile.exists())//如果讀取的文件不存在,則創(chuàng)建一個新的文件
{
listFile.createNewFile();
return;
}
input=new ObjectInputStream(new BufferedInputStream(new FileInputStream(listFile)));
//以文件名創(chuàng)建一個對象輸入流,然后將該對象加入到播放清單中
while(true)//循環(huán)將歌曲對象加入到播放列表中
{//ListValues類實現(xiàn)了接口Serializable
listWriteFile=(ListValues)input.readObject();//將對象讀入到listWriteFile類對象中
fileName.addElement(listWriteFile.getFileName());//將文件對象名加入文件名向量中
numList.addElement((numList.size()+1)+"."+listWriteFile.getFileName());//將文件對象加入清單并
//實現(xiàn)加入的對象從1一直向下排序
dirName.addElement(listWriteFile.getDirName());//將目錄名加入到目錄向量中
}
}
catch(EOFException e)
{
try
{
if(!fileName.isEmpty())
{
input.close();//確認有元素存在并加載完畢后關(guān)閉輸入流
}
}
catch(IOException e1)
{
JOptionPane.showMessageDialog(this,"文件被非正常關(guān)閉",
"非法關(guān)閉",JOptionPane.ERROR_MESSAGE);
}
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(this,"不能創(chuàng)建對象","對象創(chuàng)建失敗",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"不能讀取文件",
"讀取文件失敗",JOptionPane.ERROR_MESSAGE);
}
finally
{
try
{
if(input!=null)//輸入對象流不為空則關(guān)閉
input.close();
}
catch(IOException e)
{
}
if(dirName.isEmpty())//防止Vector越界
{
return;
}
index=(int)(Math.random()*(fileName.size()));//產(chǎn)生隨即數(shù),進行隨即播放
list.setListData(numList);//將載入文件內(nèi)容加入到list中用以后面進行保存
createPlayer2();
}
}
private void checkMenu(MouseEvent e)//標志選中項
{
if(e.isPopupTrigger())//判斷是否要求彈出一個菜單
{
indexForDel=list.locationToIndex(e.getPoint());//獲取當前鼠標點使下標轉(zhuǎn)換為用戶選中的文件
int[] selected={index,indexForDel};//建立選中數(shù)組并賦值
list.setSelectedIndices(selected);//選擇索引數(shù)組中指定的項目,并更新列表框
popupMenu.show(list,e.getX(),e.getY());//在相對于初始組件list的 x、y 位置顯示彈出式菜單
}
}
String reNames()
{
String name=JOptionPane.showInputDialog(this,"請輸入新的名字",fileName.elementAt(indexForDel));
return name;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -