小巧Java版媒体播放器

    最近软件工程做了个Java版的媒体播放器,我把代码进行了精缩了一下,嘿嘿,从上千行的代码缩成了250多行:),不过功能也少了很多哦,没有了列表,没有了文件过滤等等功能。不过麻雀虽小,五脏俱全哦,播放mp3、mpg、mpeg、avi等等音乐文件没问题哦!算是简装版啦,嘿嘿!下面是代码!

package edu.whu.bbflyerwww.mymusic;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.RealizeCompleteEvent;
import javax.media.PrefetchCompleteEvent;
import javax.media.bean.playerbean.MediaPlayer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

/**
 * 小巧的媒体播放器MyMusic(java实现)<br>
 * 基于JMF(Java Media Framework)实现<br>
 * 没有很复杂的功能,没有很漂亮的界面,仅仅娱乐用<br>
 * @environment eclipse
 * @author bbflyerwww
 * @email bbflyer@qq.com
 * @free_version
 */

public class MyMusic extends JFrame implements ControllerListener {
 MediaPlayer player; // 媒体播放器
 JPanel center; // getContentPane() 可视组件,放v_part
 JPanel south;  // getContentPane() 控制组件,放c_part
 Component v_part;
 Component c_part;
 JFileChooser chooser; // 文件选择

 /**
  * 构造函数
  */
 public MyMusic() {
  initialize();
  this.setVisible(true);
 }

 /**
  * App初始化
  */
 private void initialize() {
  this.setTitle("MyMusic");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  initJMenuBar();
  initJContentPane();
  updateUI();
  initComponents();
 }

 /**
  * 初始化菜单布局和事件
  */
 private void initJMenuBar() {
  JPopupMenu.setDefaultLightWeightPopupEnabled(false);
  final JMenu file = new JMenu("File");
  final JMenuItem fileopen = new JMenuItem("Open Local Music...");
  final JMenuItem appexit = new JMenuItem("Exit");
  file.add(fileopen);
  file.addSeparator();
  file.add(appexit);
  fileopen.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    cmdOpenMusic();
   }
  });
  appexit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    cmdAppExit();
   }
  });
  final JMenu help = new JMenu("Help");
  final JMenuItem about = new JMenuItem("About...");
  help.add(about);
  about.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    cmdAppAbout();
   }
  });
  final JMenuBar bar = new JMenuBar();
  bar.add(file);
  bar.add(help);
  this.setJMenuBar(bar);
 }

 /**
  * 初始化ContentPane
  */
 private void initJContentPane() {
  this.getContentPane().setLayout(new BorderLayout());
  center = new JPanel();
  south = new JPanel();
  center.setLayout(new BorderLayout());
  south.setLayout(new BorderLayout());
  this.getContentPane().add(center, BorderLayout.CENTER);
  this.getContentPane().add(south, BorderLayout.SOUTH);
  setJCenterBackground();
 }

 /**
  * 初始化设置窗体背景
  */
 private void setJCenterBackground() {
  final String picture = "edu//whu//bbflyerwww//image//枫中奇缘.jpg";
  final Icon icon = new ImageIcon(picture);
  final JLabel label = new JLabel(icon);
  center.add(label, BorderLayout.CENTER);
 }

 /**
  * 更新设置窗体显示的屏幕位置
  */
 private void updateJLocation() {
  Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension fsize = this.getSize();
  if (fsize.height > scr.height)
   fsize.height = scr.height;
  if (fsize.width > scr.width)
   fsize.width = scr.width;
  this.setLocation((scr.width - fsize.width) / 2,
    (scr.height - fsize.height) / 2);
 }

 /**
  * 更新整个窗口界面
  */
 private void updateUI() {
  this.pack();
  this.validate();
  updateJLocation();
 }

 /**
  * 初始化其他组件
  */
 private void initComponents() {
  player = new MediaPlayer();
  player.addControllerListener(this);
  chooser = new JFileChooser();
 }
 
 /**
  * 释放资源
  */
 private void close() {
  this.dispose();
  if (player != null) {
   player.close();
  }
 }
 
 /**
  * 关于
  */
 private void cmdAppAbout() {
  JOptionPane.showMessageDialog(this,
    "@author:bbflyerwww",
    "about",
    JOptionPane.PLAIN_MESSAGE);
 }
 
 /**
  * 退出系统
  */
 private void cmdAppExit() {
  close();
  System.exit(0);
 }
 
 /**
  * 打开本地媒体文件对话框
  */
 private void cmdOpenMusic() {
  int value = chooser.showOpenDialog(this);
  if (value == JFileChooser.APPROVE_OPTION) {
   String filename = chooser.getSelectedFile().toString();
   play(filename);
  }
 }

 /**
  * 实现播放
  * @param MediaName
  */
 private void play(String MediaName) {
  try {
   URL mediaURL = new URL("file:" + MediaName);
   play(mediaURL);
  } catch (Exception e) {
   System.out.println("您输入的文件有问题");
   e.printStackTrace();
  }
 }

 /**
  * 实现播放
  * @param url
  * @throws Exception
  */
 private void play(URL url) throws Exception {
  player.setMediaLocation(url.toString());
  player.realize();
 }

 /**
  * 实现ControllerListener接口
  * @param event
  */
 public void controllerUpdate(ControllerEvent event) {
  if (event instanceof RealizeCompleteEvent) {
   if (v_part != null)
    center.remove(v_part);
   if ((v_part = player.getVisualComponent()) != null)
    center.add(v_part, BorderLayout.CENTER);
   else
    setJCenterBackground();
   if (c_part != null)
    south.remove(c_part);
   if ((c_part = player.getControlPanelComponent()) != null)
    south.add(c_part, BorderLayout.SOUTH);
   player.prefetch();
  }
  if (event instanceof PrefetchCompleteEvent) {
   updateUI();
   player.start();
  }
 }

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

}

  以下抓几个图啦,嘿嘿

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值