⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaplayer.java

📁 java播放器 java播放器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	       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 + -