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

📄 mediaplayer.java

📁 just for test.a program is based on JMF.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** 
  this program is just for playing music of .mp3
*/
import java.awt.*;
import java.awt.List;
import javax.media.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.net.*;
import java.io.*;             //file out and in

public class MediaPlayer extends JFrame{
   Component ac=null;                                //just one component
   ListAction1 list1=null;
   ListAction2 list2=null;
   Play1 playall=null;  
  // int clicked=0;
   Play2 playall1=null;           
   Player audioPlayer=null;       //create a player object.
   Container contain=getContentPane();   
   JMenuBar  menubar;           
   JMenu[] menus={new JMenu("File"),new JMenu("Play"),new JMenu("PlayList")};
   JMenuItem[] menuitemsoffile={new JMenuItem("Open"),new JMenuItem("New list"),new JMenuItem("Edit"),
                                        new JMenuItem("Exit")};
   JMenuItem[] menuitemsofplay={new JMenuItem("Play"),new JMenuItem("Pause"),new JMenuItem("Previous"),
                                        new JMenuItem("Next"),new JMenuItem("Stop")};
   MediaPlayer(){
     super("Audio Player(.mp3)");
     menubar=new JMenuBar();
     
     for(int i=0;i<menuitemsoffile.length;i++)    //add menuitems to 'file'
        menus[0].add(menuitemsoffile[i]);
     
     for(int i=0;i<menuitemsofplay.length;i++)    //add menuitems to 'play'
        menus[1].add(menuitemsofplay[i]);
     
     for(int i=0;i<menus.length;i++)
        menubar.add(menus[i]);
     setJMenuBar(menubar);
     
     menuitemsofplay[2].setEnabled(false);
     menuitemsofplay[3].setEnabled(false);
     setSize(300,90);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     setResizable(false);
     setVisible(true);

     /*this is to add actionListener so that systems can echo users' requirement! */

     for(int i=0;i<menus[0].getItemCount();i++)       //add actionlisener to 'file'
         menuitemsoffile[i].addActionListener(new ActionHandler1(this));

     for(int i=0;i<menus[1].getItemCount();i++)       //add actionListener to 'play'
         menuitemsofplay[i].addActionListener(new ActionHandler1(this));
     menus[2].addMenuListener(new ActionHandler2());
    
    
  }  
  class ActionHandler2 implements MenuListener{
  	  public void menuSelected(MenuEvent me){
  	    if(me.getSource()==menus[2])
                if(list2!=null)
                   return;
                else
  	  	  list2=new ListAction2();
  	  	if(list2!=null){
  	  	  menuitemsofplay[2].setEnabled(true);
  	  	  menuitemsofplay[3].setEnabled(true);
  	    }
  	    else{
  	  	  menuitemsofplay[2].setEnabled(false);
  	  	  menuitemsofplay[3].setEnabled(false);
  	   }
  	  	  
  	  	
          }
  	  public void menuDeselected(MenuEvent me){}
  	  public void menuCanceled(MenuEvent me){}
   }
  

  /*this class is to deal with actions users required!*/
  class ActionHandler1 implements ActionListener{
     JFrame frame;
     String actionName=null;
     
     ActionHandler1(JFrame frame){
       this.frame=frame;
     }
    
     public void actionPerformed(ActionEvent ae){
        actionName=ae.getActionCommand();           //get menu-instruction that it's just clicked by user.
        String filename=null;
        String filepath=null;
        
        if(actionName.equals("Open")){             //button "Open"
           FileDialog musicfile=new FileDialog(frame,"open",FileDialog.LOAD); 
           musicfile.setVisible(true);
           filename=musicfile.getFile();
           filepath=musicfile.getDirectory(); 
           if(filename!=null&&filepath!=null)
             playall=new Play1(filename,filepath);
           if(playall!=null)
             playall.createPlayer();
         }

        if(actionName.equals("New list")){
         if(list1!=null)
           return;
          else
            list1=new ListAction1();             //????????????????????????????????????????
        }
 
        if(actionName.equals("Edit")){
          if(list2!=null){
            menuitemsofplay[2].setEnabled(true);
            menuitemsofplay[3].setEnabled(true);
            }
           else{
           	menuitemsofplay[2].setEnabled(false);
           	menuitemsofplay[3].setEnabled(false);
           }
             
          if(list2!=null)
            return;
         else
          list2=new ListAction2();
          
        }
        
        if(actionName.equals("Exit")){ 
          if(audioPlayer!=null)
              audioPlayer.close();
          System.exit(0);
        }

       if(actionName.equals("Play")){
         if(audioPlayer!=null){
           audioPlayer.setMediaTime(audioPlayer.getMediaTime());
           audioPlayer.start();
          }
         else
           JOptionPane.showMessageDialog(null,"No Player was created!","Caution",JOptionPane.ERROR_MESSAGE);
           /*show a messagebox to screen indicates that no player was created.*/
       }
                   
       if(actionName.equals("Pause")){

          if(audioPlayer!=null) 
            audioPlayer.stop();
          else
            JOptionPane.showMessageDialog(null,"No Player was created!","Caution",JOptionPane.ERROR_MESSAGE);
       }

       if(actionName.equals("Next")){
          if(playall1.no==playall1.listcontent.getItemCount()){     //test if it's bottom !
       	     JOptionPane.showMessageDialog(null,"it's bottom of current list!","Error",JOptionPane.ERROR_MESSAGE);
       	     return;
       	  }
          if(audioPlayer!=null)
            audioPlayer.close();
          ++playall1.no;
          playall1.createplayer();
        
       }
       if(actionName.equals("Previous")){
         if(playall1.no==0){
          JOptionPane.showMessageDialog(null,"it's the top of current list!","Error",JOptionPane.ERROR_MESSAGE);
          return;
         }

          if(audioPlayer!=null)
            audioPlayer.close();
          --playall1.no;
          playall1.createplayer();
       }

       if(actionName.equals("Stop")){
        if(audioPlayer!=null){
          audioPlayer.close();
      } 
   }
  }         
  } //the end of  action-handler.
 class ListAction1 extends JFrame implements ActionListener{
   private BufferedWriter filewriter;
   private String savename;
   private FileDialog fopen;
   private JLabel listname,listcontent;
   private JTextField  nameoflist;
   private Container contain=getContentPane();
   private JTextArea contentoflist;
   private JButton button1,button2,button3;
   private JScrollPane contentscrollpane;
   private int para1=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
   private int para2=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
   ListAction1(){
     super("New List");

     listname=new JLabel("Name:");
     listcontent=new JLabel("Content:");
     nameoflist=new JTextField();
     contentoflist=new JTextArea();
     contentoflist.setEditable(false);
     contentscrollpane=new JScrollPane(contentoflist,para1,para2);
        
     button1=new JButton("Open");
     button2=new JButton("Ok");
     button3=new JButton("Cancel");
     contain.setLayout(null);
      
     listname.setBounds(10,10,80,25);
     nameoflist.setBounds(75,10,170,28);
    
     listcontent.setBounds(10,45,100,40);
     button1.setBounds(217,90,70,30);
     button2.setBounds(217,125,70,30);
     button3.setBounds(217,160,75,30);
     contentscrollpane.setBounds(10,90,200,150);
     
     contain.add(listname);
     contain.add(nameoflist);
     contain.add(listcontent);
     contain.add(contentscrollpane);
     contain.add(button1);
     contain.add(button2);
     contain.add(button3);
     button2.setEnabled(false);     
    /**we should pay more attention to this proble
      if we add this sentence "setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); " here,
      we will close parent window.so we can't go on our actions again!
    */
     setSize(300,300);
     setVisible(true);
     
     button1.addActionListener(this);
     button2.addActionListener(this);
     button3.addActionListener(this);
     this.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent w){
           list1=null;
           dispose();
        }
     });
   }
    
    public void actionPerformed(ActionEvent ae){
      String actionCommand=ae.getActionCommand();
      String name=null,path=null;
      savename=nameoflist.getText().trim();
      
      if(actionCommand.equals("Open")){                     //button "open"
        fopen=new FileDialog(this,"open",FileDialog.LOAD);
        fopen.setVisible(true);
        name=fopen.getFile();
        path=fopen.getDirectory();
        if(name.length()!=0&&path.length()!=0){
          contentoflist.append(path+name+"\n");
          button2.setEnabled(true);
        }
        else {
           JOptionPane.showMessageDialog(null,"unspecified File or Path!","Caution",JOptionPane.ERROR_MESSAGE);
           return;
        }
        
        
      }

     if(actionCommand.equals("Ok"))
      if(savename.length()==0)
         JOptionPane.showMessageDialog(null,"No name!","Caution",JOptionPane.ERROR_MESSAGE);
      else{
        try{
           filewriter=new BufferedWriter(new FileWriter("PlayList/"+savename+".txt"));
             filewriter.write(contentoflist.getText()); 
             filewriter.close();
          }catch(IOException ie){
           JOptionPane.showMessageDialog(null,"I/O ERROR!","Caution",JOptionPane.ERROR_MESSAGE);
          }finally{
            this.dispose();
          }
      }
      
      if(actionCommand.equals("Cancel")){
        try{
          if(filewriter!=null) filewriter.close();
        }catch(IOException ioe){}
         this.dispose();
        }
      
      }
    }

⌨️ 快捷键说明

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