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

📄 mediaplayer.java

📁 just for test.a program is based on JMF.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   
  
 class ListAction2 extends JFrame implements ActionListener{
   private JLabel listname;
   private File listnames=new File("PlayList/");
   private String[] playlistnames=listnames.list();   //get all files
   private JComboBox listnamesequence;
   private List listcontent;                                
   private int para1=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
   private int para2=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
   private BufferedWriter fwriter;
   private BufferedReader freader;
   private int clicked;
   private Container contain=getContentPane();
   private JButton[] button={new JButton("Play"),new JButton("add"),new JButton("remove"),new JButton("removeA"),new JButton("ok"),new JButton("Cancel")};
  
   ListAction2(){
    super("List");
    clicked=0;
    listname=new JLabel("Name:");
    listnamesequence=new JComboBox();
    listcontent=new List();
  
    
    contain.setLayout(null);
    
    listname.setBounds(10,10,70,25);
    listnamesequence.setBounds(85,10,200,25);
    listcontent.setBounds(10,150,220,200);
    for(int i=0;i<button.length;i++)
      button[i].setBounds(250,150+i*25+5,90,30);

    for(int i=0;i<playlistnames.length;i++)
      listnamesequence.addItem(playlistnames[i]);

    contain.add(listname);
    contain.add(listnamesequence);
    contain.add(listcontent);
    for(int i=0;i<button.length;i++)
     contain.add(button[i]);

    
    setSize(400,400); 
    setVisible(true);
    setResizable(false);

    //register actionListener to buttons and combo box  
    for(int i=0;i<button.length;i++)
      button[i].addActionListener(this);
    listnamesequence.addActionListener(this);
    this.addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent we){ 
         list2=null;                                      
         dispose();
       }
     });
   }

   public void actionPerformed(ActionEvent ae){          //button actions 
    
      String actioncommand=ae.getActionCommand();
      if(actioncommand.equals("Play")){                    //play in ListAction2
         if(audioPlayer!=null)          //test audioPlayer is null or not and,take actions if it is not null!
              audioPlayer.close();
       
         if(playall1!=null)
           playall1=null;                   //test playall1 object
        
      
         playall1=new Play2(list2.listcontent);   //???????????????????????????????????
         int i=playall1.calculate();
         if(i==0)                        //if listcontent has no records,then no operations is executed!
           return;
         else 
           playall1.createplayer();
      	
      }  

      if(actioncommand.equals("add")){
        FileDialog fopen=new FileDialog(this,"open",FileDialog.LOAD);
        fopen.setVisible(true);
        String filepath=fopen.getDirectory();
        String filename=fopen.getFile();

        if(filepath!=null&&filename!=null)
          listcontent.add(filepath+filename);
        else
          JOptionPane.showMessageDialog(null,"unspecified file/path!","Error",JOptionPane.ERROR_MESSAGE);
      }


      if(actioncommand.equals("remove")){               //button 'remove'
         int no=listcontent.getSelectedIndex();
         if(no<listcontent.getItemCount())
           listcontent.remove(no);
         else
           JOptionPane.showMessageDialog(null,"Without the record!","Error",JOptionPane.ERROR_MESSAGE);
       }

      if(actioncommand.equals("removeA")){                  //button 'removeA'
       if(listcontent.getItemCount()!=0)
         listcontent.removeAll();        
      }


      if(actioncommand.equals("ok")){                           //button 'ok'
        String item=(String)listnamesequence.getSelectedItem(); //get list-name;
        if(item!=null)
          try{
           
            fwriter=new BufferedWriter(new FileWriter("PlayList/"+ item)); 
            //create connection to interconnected file.
            for(int i=0;i<listcontent.getItemCount();i++)
              fwriter.write(listcontent.getItem(i)+"\n");  //sent content to Playlist file
          
            
          }catch(IOException ie){
          }finally{
           try{
             fwriter.close();                       //close output stream.
            }catch(IOException ie4){
              JOptionPane.showMessageDialog(null,"I/O ERROR!","Error",JOptionPane.ERROR_MESSAGE);
            }
         } 
          this.dispose();
         
       }


      if(actioncommand.equals("Cancel")){         //button 'Cancel'
         this.dispose();
      }
     
    if(ae.getSource() instanceof JComboBox){
      String filen=(String)listnamesequence.getSelectedItem();   //get name of play list<treat it as a file>
      String content=null;
      listcontent.removeAll();
      try{
        freader=new BufferedReader(new FileReader("PlayList/"+filen));  //gets content of fixed file<Playlist name>
        content=freader.readLine();
        if(content==null){
          JOptionPane.showMessageDialog(null,"No any records!","Error",JOptionPane.INFORMATION_MESSAGE);
          return;
       }
       while(content!=null){
          listcontent.add(content); 
          content=freader.readLine();
       }
       
     }catch(IOException ie2){
       JOptionPane.showMessageDialog(null,"I/O Exception,Please Check!","Error",JOptionPane.ERROR_MESSAGE);
     }finally{
        try{ 
          freader.close();
        }catch(IOException ie1){}
       }  
      }
      
     }
   }  

   class Play2 implements ControllerListener{
     private List listcontent;
     private int no,total;
     private String filen;
     private File fileURL;
     Play2(List listcontent){
       this.listcontent=listcontent;
     }
     int calculate(){                               //????????????????????????????????????????????????????
       total=listcontent.getItemCount();            //get total number of items
       if(total==0){
          JOptionPane.showMessageDialog(null,"No music!","Error",JOptionPane.ERROR_MESSAGE);
          return 0;
       }
      
       //choose one item to do action or not to choose,
       filen=listcontent.getSelectedItem();
       if(filen==null)                                    //fix the no-th item!
          no=0;
       else
          no=listcontent.getSelectedIndex();
       return 1;
     }
     
     void createplayer(){
     	  //total=listcontent.getItemCount();
       
        listcontent.select(no);            //set background of chosen item.
        filen=listcontent.getItem(no);
        try{
          fileURL=new File(filen);
          audioPlayer=Manager.createRealizedPlayer(fileURL.toURL());   
         }catch(IOException ie1){
         }catch(NoPlayerException ie2){
         }catch(CannotRealizeException ie3){
         }           
       if(audioPlayer!=null){
         audioPlayer.addControllerListener(this);
         audioPlayer.realize();
         audioPlayer.prefetch();
       }
      else
        return;
      }
      public void controllerUpdate(ControllerEvent ce){
         if(ce instanceof PrefetchCompleteEvent){
            audioPlayer.start();
            ac=audioPlayer.getControlPanelComponent();
            ac.setSize(295,30);
            contain.add(ac,BorderLayout.CENTER);

         }
         if(ce instanceof EndOfMediaEvent){
            no++;
            if(no<listcontent.getItemCount()){                   //get next element
               audioPlayer.close();
               createplayer(); 
            }
           else {                                                //elements done
             audioPlayer.close();
             return;
           } 
         }
         if(ce instanceof ControllerClosedEvent){ 
           if(ac!=null){
              contain.remove(ac);
              ac=null;
           }
            
         }
       }      
     }

  class Play1 implements ControllerListener{
     private String filename,filepath;
     private String fpath;
     //private Component ac;      //control component....
     //private Player audioPlayer;
     Play1(String fpath){
       this.fpath=fpath;
     }
     Play1(String filename,String filepath){
      this.filename=filename;
      this.filepath=filepath;
      this.fpath=filepath+filename;
     }
     public void createPlayer(){
       if(audioPlayer!=null)
        audioPlayer.close();
        
      if(fpath!=null){
        try{
          File fi=new File(fpath);
          audioPlayer=Manager.createRealizedPlayer(fi.toURL());
          if(audioPlayer!=null){
            audioPlayer.addControllerListener(this);
            audioPlayer.realize();
            audioPlayer.prefetch();  //file prefetched!
            
          }
        }catch(IOException ie){
        }catch(NoPlayerException ne){
          JOptionPane.showMessageDialog(null,"No player was created!","caution",JOptionPane.ERROR_MESSAGE);
          return;
       }catch(CannotRealizeException e){}
         
      }
  
    }

    public void controllerUpdate(ControllerEvent ce){
      if(ce instanceof PrefetchCompleteEvent){   
         if(audioPlayer!=null) {                        
            ac=audioPlayer.getControlPanelComponent();
            ac.setSize(295,30);
         }
         contain.add(ac,BorderLayout.CENTER);
         audioPlayer.start();
 
      }
    
     if(ce instanceof ControllerClosedEvent){
       if(ac!=null){
       	 contain.remove(ac);
         ac=null;
       }
     }

   }
  }
   public static void main(String[] args){
     new MediaPlayer();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -