?? mediaplayer.java
字號(hào):
* int result=fileChooser.showOpenDialog(this);//創(chuàng)建文件打開對(duì)話框,并設(shè)定此程序?yàn)楦复翱诒O(jiān)控*/
/*通過result的值來判斷文件是否打開成功
* *JFileChooser類有很多靜態(tài)成員變量
* **/
/*if(result==JFileChooser.CANCEL_OPTION)
* {
* file=null;//file已經(jīng)在類中定義,如果選擇取消,file指向?yàn)榭?
* }
* else
*
* {
* file=fileChooser.getSelectedFile();//獲得文件對(duì)象
* title=file.getAbsolutePath();//取得文件的絕對(duì)路徑并且賦給title設(shè)定標(biāo)題
* }*/
//if(fd==null)
//{
//String filename="java音頻播放器";
fd = new FileDialog(MediaPlayer.this);
fd.setVisible(true);
if (fd.getFile() != null)
{
title = fd.getDirectory() + fd.getFile();//原因請見同目錄下的FileDialogDemo.java文件
files=fd.getFile();
//dir =fd.getDirectory();
file=new File(title);
createPlayer();
}
//title=filename;
//fd=null;//缺少此句如果第一次打開文件的時(shí)候取消操作的時(shí)候第二次也不能打開文件了
//}
}
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,"錯(cuò)誤的路徑",
"出錯(cuò)了",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);
/*fd=new FileDialog(MediaPlayer.this);
* fd.setVisible(true);
* if(fd.getDirectory()!=null)
* {
* File fileDir=new File(fd.getDirectory());
* String[] ss=fileDir.list();
* for(int i=0;i<ss.length;i++)
* {
* System.out.println(ss[i]);
* }
* }*/
}
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);
//list.setSelectedIndex(index);
//list.setSelectionForeground(Color.blue);
setTitle(title);
//addList("files");//到播放清單
//title="file.getAbsoluteFile()";
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,extendName,"出錯(cuò)了!!",JOptionPane.ERROR_MESSAGE);
setTitle(extendName);
}
}
private void closePreviosPlayer()
{
if(player==null)
return;
//player.close();//首先停止播放
/*
* *不能用上面的代碼停止,要用下面的兩行取代,否則Component visual =player.getVisualComponent();發(fā)生異常
* **/
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(); //一個(gè)播放所有視頻媒體的可視組件
if(visual!=null)
{
System.out.println("音頻播放器不支持視頻圖象功能");
setTitle("音頻播放器不支持視頻圖象功能");
//panel.removeAll();
//panel.add(visual,BorderLayout.CENTER);
}
/*else
* {
* panel.add(label,BorderLayout.CENTER);
* }*///此else語句可以防止因?yàn)樵瓉聿シ乓曨l圖象后以后沒有界面
Component control=player.getControlPanelComponent();//一個(gè)操作時(shí)間軸的可視組件(包括開始,停止,回放),也包含了一些媒體流的有用信息
//Component voice=player.getGainControl().getControlComponent();
if(control!=null)
{
panel.add(control,BorderLayout.SOUTH);
}
//c.validate();
panel.doLayout();
return;
}
if (e instanceof EndOfMediaEvent)
{
/*if (loop)
* {
* player.setMediaTime (new Time (0));
* player.start ();
* }
* return;*/
if(buttonValues[0].isSelected())
{
if(fileName.size()==0)
return;
index=(int)(Math.random()*fileName.size());
}
if(buttonValues[1].isSelected())
{
if(fileName.size()==0)
return;//必須有此if語句,否則當(dāng)用戶把播放列表清空的時(shí)候發(fā)生異常,偶然的機(jī)會(huì)發(fā)現(xiàn)的
//現(xiàn)在感覺測試軟件真是太重要了,看來以后程序做好后要反復(fù)測試,考慮各種情況
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==null)
if(exi==JOptionPane.YES_OPTION)
{
saveList();
setVisible(false);
}
return;
}
private void addList(String vf)
{
//fileReadList=new fileReadList(fdd,)
//try
//{
//int i=0;
//fileName.addElement((fileName.size()+1)+"."+vf);
fileName.addElement(vf);
numList.addElement((numList.size()+1)+"."+vf);
//fileName.addElement(++i+"."+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();
}
//title=dirName.elementAt(index).toString();
catch(ArrayIndexOutOfBoundsException e)
{return;}
file=new File(title);
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();
//list.setSelectedIndex(index);
list.setSelectedValue(numList.elementAt(index),true);
list.setSelectionForeground(Color.blue);
//list.setSelectedIndex(index);
//addList(files);
setTitle(title);
//addList("files");//到播放清單
}
catch(Exception e)
{
//JOptionPane.showMessageDialog(this,extendName,"出錯(cuò)了!!",JOptionPane.ERROR_MESSAGE);
//setTitle(extendName);
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(fileName);
System.out.println("已經(jīng)從播放列表中刪除 "+"\""+ex+"\""+" 文件,"
+"因?yàn)榇瞬シ牌鞑恢С?quot;+ex.substring(ex.lastIndexOf(".")+1)+"格式,"
+"不過沒有從硬盤真正刪除");
if(numList.size()!=0)
{
index%=numList.size();
}
}
}
private void saveList()
{
Enumeration enumFile=fileName.elements();
Enumeration enumDir =dirName.elements();
try
{
output=new ObjectOutputStream(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();
}
/*finally
* {
* output.flush();
* output.close();//郁悶,這兩行不能寫在這里,實(shí)在是一大遺憾啊,不知道有什么別的方法
* }*/
}
public void run()
{
try
{
Thread.sleep(1);
}
catch(InterruptedException e)
{
}
try
{
if(!listFile.exists())
listFile.createNewFile();//防止不存在此文件發(fā)生讀取錯(cuò)誤,這兩行代碼保證不存在的情況下自動(dòng)建立
input=new ObjectInputStream(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
{
if(!fileName.isEmpty())
input.close();//確認(rèn)有元素存在并加載完畢后關(guān)閉輸入流
}
catch(IOException e1)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"文件被非正常關(guān)閉",
"非法關(guān)閉",JOptionPane.ERROR_MESSAGE);
}
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"不能創(chuàng)建對(duì)象","對(duì)象創(chuàng)建失敗",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(MediaPlayer.this,"不能讀取文件",
"讀取文件失敗",JOptionPane.ERROR_MESSAGE);
}
finally
{
if(dirName.isEmpty())//防止Vector越界
{
return;
}
index=(int)(Math.random()*(fileName.size()));//產(chǎn)生隨即數(shù),進(jìn)行隨即播放
list.setListData(numList);
//list.setListData(fileName);
//list.setSelectedValue(fileName.elementAt(index),true);
//list.ensureIndexIsVisible(index);//確保選擇項(xiàng)可以看見
//list.setSelectionForeground(Color.green);
createPlayer2();
}
}
private void checkMenu(MouseEvent e)
{
if(e.isPopupTrigger())
{
indexForDel=list.locationToIndex(e.getPoint());
int[] selected={index,indexForDel};
//Point p=new Point(e.getX(),e.getY());
list.setSelectedIndices(selected);
popupMenu.show(list,e.getX(),e.getY());
}
//list.setSelectedIndex(index);
}
String reNames() throws ReName//文件該函數(shù)
{
String name=JOptionPane.showInputDialog(this,"請輸入新的名字",fileName.elementAt(indexForDel));
if(name==null||name.equals("")) throw new ReName();
//必須把name==null放在前面,否則會(huì)發(fā)生NullPointerExceptin,這個(gè)很好理解,
//當(dāng)按了取消后,name會(huì)成為空,那么name.equals("")就會(huì)發(fā)生異常
return name;
}
class ReName extends Exception//自定義異常來處理文件該名的時(shí)候發(fā)生輸入為空的情形
{
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -