java 音乐播放器

   1:  import java.awt.*;
   2:  import java.awt.event.*;
   3:  import java.net.*;
   4:  import javax.swing.*;
   5:  import java.io.*;
   6:   
   7:  import javax.media.*;
   8:  public class MediaPlayer extends JFrame implements ActionListener,ControllerListener,Runnable,ItemListener 
   9:  {
  10:      Player player=null;    
  11:      Thread thread;
  12:      File file,dir;
  13:      FileDialog fileDialog;
  14:      URL url;
  15:      boolean loop=false;
  16:      String filename[]={};
  17:      Menu playListMenu;
  18:      MenuItem openItem,exitItem,playItem,pauseItem,stopItem,playListItem;
  19:      CheckboxMenuItem circulateItem;
  20:   
  21:      // 构造函数,其中包括了设置响应窗口事件的监听器。 
  22:      MediaPlayer(String title) {
  23:          super(title);
  24:          thread=new Thread(this);
  25:          //  关闭按钮的实现 
  26:          addWindowListener(new WindowAdapter() {
  27:              public void windowClosing(WindowEvent e) {
  28:                  dispose();
  29:                  System.exit(0);
  30:              }
  31:          });
  32:          // 调用程序菜单栏的方法成员完成菜单的布置 
  33:          setupMenu();
  34:          setSize(300, 300);
  35:          setBounds(300,300,300,80);
  36:          setResizable(false);
  37:          setVisible(true);
  38:          validate();
  39:      }
  40:      /**
  41:       * 本方法用以设置程序菜单栏
  42:       */
  43:      public void setupMenu() {
  44:          MenuBar menuBar = new MenuBar();
  45:          setMenuBar(menuBar);
  46:          //文件菜单
  47:          Menu fileMenu = new Menu("文件");
  48:          menuBar.add(fileMenu);
  49:          //打开按钮
  50:          openItem = new MenuItem("打开");
  51:          fileMenu.add(openItem);
  52:          openItem.addActionListener(this);
  53:          fileMenu.addSeparator();
  54:          //导入播放列表
  55:          playListItem=new MenuItem("播放列表");
  56:          playListItem.addActionListener(this);
  57:          fileMenu.add(playListItem);
  58:          fileMenu.addSeparator();
  59:          //退出按钮
  60:          exitItem = new MenuItem("退出");
  61:          exitItem.addActionListener(this);
  62:          fileMenu.add(exitItem);
  63:          fileMenu.addSeparator(); 
  64:          //播放控制菜单
  65:          Menu playControlMenu = new Menu("播放控制");
  66:          menuBar.add(playControlMenu);
  67:          //播放按钮
  68:          playItem = new MenuItem("播放");
  69:          playItem.addActionListener(this);
  70:          playControlMenu.add(playItem);
  71:          playControlMenu.addSeparator();
  72:          //暂停按钮
  73:          pauseItem = new MenuItem("暂停");
  74:          pauseItem.addActionListener(this);
  75:          playControlMenu.add(pauseItem);
  76:          playControlMenu.addSeparator();
  77:          //停止按钮
  78:          stopItem = new MenuItem("停止");
  79:          stopItem.addActionListener(this);
  80:          playControlMenu.add(stopItem);
  81:          playControlMenu.addSeparator();
  82:          //循环按钮
  83:          circulateItem = new CheckboxMenuItem("循环", false);
  84:          circulateItem.addActionListener(this);
  85:          playControlMenu.add(circulateItem);
  86:   
  87:          //播放列表
  88:          playListMenu = new Menu("播放列表");
  89:          menuBar.add(playListMenu);
  90:      }
  91:   
  92:      /**
  93:       *  动作时间响应成员
  94:       *  捕捉发送到本对象的各种事件
  95:       */
  96:      public void actionPerformed(ActionEvent e) {
  97:          // 调用dispose以便执行windowClosed
  98:          if (e.getSource()== exitItem){   
  99:              dispose();
 100:              player.stop();
 101:              System.exit(0);
 102:              return;
 103:          }
 104:          //此事表明拥护选择了“播放”命令; 如果当前有一个文件可以播放则执行播放命令;
 105:          else if (e.getSource()==playItem) { 
 106:              if (player != null) 
 107:                  player.start();
 108:              return;
 109:          }
 110:          // 如果当前正在播放某一文件,则执行暂停; 
 111:          else if (e.getSource()==pauseItem) {
 112:              if (player != null) 
 113:                  player.stop();
 114:              return;
 115:          }
 116:          // 停止命令的响应,把时间进度条恢复到开始,并再次重新开始播放 
 117:          else if (e.getSource()==stopItem) {
 118:              System.out.println("stopItem");
 119:              if (player != null) {
 120:                  player.stop();
 121:                  player.setMediaTime(new Time(0));
 122:              }
 123:              return;
 124:          }
 125:          // 用户选择要播放的媒体文件 
 126:          else if (e.getSource()==openItem) {
 127:              System.out.println("打开按钮");
 128:              fileDialog=new FileDialog(this,"打开音乐文件",FileDialog.LOAD);
 129:              fileDialog.setVisible(true);
 130:              file=new File(fileDialog.getDirectory(),fileDialog.getFile());
 131:              if(file==null)
 132:                  return;
 133:              if (player != null) {
 134:                  System.out.println("无法创建播放器");
 135:                  player.stop();
 136:                  player.deallocate();
 137:              }
 138:              if(!(thread.isAlive()))
 139:                  thread=new Thread(this);
 140:              try 
 141:              {
 142:                  thread.start();
 143:              } catch (Exception ex) {}
 144:              removeAll();
 145:          }
 146:          else if(e.getSource()==playListItem){
 147:              JFileChooser filechooser=new JFileChooser();
 148:              filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 149:              filechooser.showOpenDialog(this);
 150:              if(filechooser.getSelectedFile()!=null){
 151:                  dir=filechooser.getSelectedFile();
 152:                  myFileFilter acceptedFile=new myFileFilter("mp3");
 153:                  filename=dir.list(acceptedFile);
 154:              }
 155:              for(int i=0;i<filename.length;i++){
 156:                  MenuItem item=new MenuItem(filename[i]);
 157:                  item.addActionListener(this);
 158:                  playListMenu.add(item);
 159:              }
 160:          }
 161:          else{
 162:              MenuItem tempItem=(MenuItem)e.getSource();
 163:              try{
 164:                  File myFile=new File(dir,tempItem.getLabel());
 165:                  System.out.println(tempItem.getLabel());
 166:                  if(player!=null){
 167:                      player.stop();
 168:                      player.deallocate(); 
 169:                  }
 170:                  if(!(thread.isAlive()))
 171:                      thread=new Thread(this);
 172:                  try   
 173:                  {
 174:                      thread.start();   
 175:                  }   
 176:                  catch(Exception e4) {} 
 177:                  removeAll();
 178:                  player=Manager.createPlayer(new MediaLocator("file:"+myFile));
 179:                  player.getDuration();
 180:                  player.prefetch();
 181:                  player.addControllerListener(this);
 182:                  setTitle(myFile.getName());
 183:              }catch(Exception ex){}
 184:          }
 185:      }
 186:   
 187:      // 菜单状态改变事件的响应函数; 
 188:      public void itemStateChanged(ItemEvent e) {
 189:          if(loop) 
 190:              loop=false;
 191:          else     
 192:              loop=true;
 193:      }
 194:      public  synchronized void run()   
 195:      {     
 196:          try   
 197:          {    
 198:              System.out.println("Thread");
 199:              player=Manager.createPlayer(new MediaLocator("file:"+fileDialog.getDirectory()+fileDialog.getFile()));   
 200:              player.getDuration();   
 201:              player.prefetch();   
 202:              player.addControllerListener(this);   
 203:              setTitle(file.getName());   
 204:          }     
 205:          catch (IOException e3) {}   
 206:          catch(NoPlayerException e2) {}   
 207:      } 
 208:   
 209:      public synchronized void controllerUpdate(ControllerEvent event) {
 210:          player.getDuration();
 211:          setBackground(Color.white);
 212:          Component visualComponent,controlComponent;
 213:          if(event instanceof RealizeCompleteEvent)   
 214:          { 
 215:              if((visualComponent=player.getVisualComponent())!=null)   
 216:                  add(visualComponent,BorderLayout.CENTER);   
 217:              if((controlComponent=player.getControlPanelComponent())!=null)   
 218:                  if(visualComponent!=null)   
 219:                      add(controlComponent,BorderLayout.SOUTH);   
 220:                  else       
 221:                      add(controlComponent,BorderLayout.CENTER);      
 222:          }
 223:          else if(event instanceof PrefetchCompleteEvent)   
 224:          {    
 225:              player.start();   
 226:          }   
 227:          else if(event instanceof EndOfMediaEvent)   
 228:          {
 229:              player.setMediaTime(new Time(0));   
 230:              if(loop)   
 231:                  player.start();   
 232:          }   
 233:          validate();
 234:      }
 235:   
 236:      class myFileFilter implements FilenameFilter
 237:      {
 238:          String str=null;
 239:          myFileFilter(String s)
 240:          {
 241:              str="."+s;
 242:          }
 243:          public boolean accept(File dir, String name) {
 244:              return name.endsWith(str);
 245:          }
 246:      }
 247:   
 248:      public static void main(String[] args) {
 249:          new MediaPlayer("MP3播放器");
 250:      }
 251:   
 252:  }

主要是抄别人的。

转载于:https://www.cnblogs.com/ZJUT-jiangnan/archive/2013/06/05/3118191.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值