小巧Java版媒體播放器

來源:互聯網
上載者:User

    最近軟體工程做了個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();
 }

}

  以下抓幾個圖啦,嘿嘿

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.