超正點的音樂播放器




類有點多,就貼一個主窗口類的代碼出來吧,寫了一個星期,總算是搞出了點成果,嘿嘿。。。

主要功能:

註冊,登錄,播放音樂,搜索音樂,下載音樂,登錄後可獲得更多權限的服務

設置的功能主要有:微窗,換皮膚,透明度,觸鍵音效,按鍵音效,透明度,心情驛站(寫並且保存心情),另外還有一個便籤的功能。

播放音樂,搜索音樂,下載音樂,和便籤功能可以在未登錄的狀態下使用,其它的高級功能必須在登錄之後纔有效。

此外可以往播放列表中添加歌曲和刪除歌曲,設置播放模式(隨機,順序,單曲循環,列表循環)

大概就是這樣了,不說了,貼代碼

package rgy.com.entity;

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.GainControl;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.Time;
import javax.print.DocFlavor.STRING;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.sun.awt.AWTUtilities;

@SuppressWarnings("restriction")
public class MainFrame2 extends JFrame {
	// 添加屬性
	int xOld = 0;
	int yOld = 0;
	//
	JLabel label_close, label_mini;
	JPanel panel_close, panel_mini;
	JLabel label_login, label_register;
	JTextField tf_search;
	JLabel label_search;
	JLabel label_username;
	JLabel label_free, label_download, label_local;
	JPanel panel_pbar;// 進度面板
	JSlider slider_progress;
	JLabel label_fast, label_slow;
	JLabel label_currenttime, label_alltime;
	JLabel label_startAndPause, label_former, label_next, label_volume;
	VolumeDialog vDialog;
	JLabel label_OFFandON;
	JLabel label_yueyinBox;
	//
	BoxDialog boxDialog;
	MoodBoxDialog moodBoxDialog;
	SmallFrame smallFrame;
	NotepadDialog notepadDialog;
	SkinBoxDialog skinBoxDialog;
	RegisterDialog registerDialog;
	LoginDialog loginDialog;
	// 歌曲列表
	JList<String> list;
	DefaultListModel<String> listModel;
	JPopupMenu popupmenu;
	JMenuItem popmenuitem[];
	//
	boolean switchControl = false;// 悅音軒大門的開關
	//
	boolean flag_touch = false;
	boolean flag_click = false;
	//
	Player player = null;
	GainControl gc = null;
	//
	double second_all, second_current;
	int progress = 0;
	//
	int music_count = 0;
	//
	UpdateProgressThread th = null;
	//
	public final static int model_random = 0;// 隨機播放
	public final static int model_order = 1;// 順序播放
	public final static int model_onecircle = 2;// 單曲循環
	public final static int model_listcircle = 3;// 列表循環
	int playModel = model_onecircle;// 默認爲順序播放
	//
	int volume = 70;// 音量
	int transparence = 100;// 透明度
	//
	JLabel background;
	String skinpic=null;
	//
	boolean isSuccess=false;
	//
	public MainFrame2(String str) {
		super(str);
		this.setBounds(420, 160, 635, 400);
		// 添加背景圖片
		((JPanel) this.getContentPane()).setOpaque(false);
		ImageIcon image = new ImageIcon("img/background.jpg");
		background = new JLabel(image);
		this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
		background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		// 添加功能代碼
		initSet();// 初始化設置
		createInterface();// 設計界面
		initList();// 初始化列表
		registerEvent();// 管理所有註冊事件
	}

	public void initSet() {
		File file = new File("E:\\113Player\\save\\set.rgy");
		if (file.exists()) {
			try {
				FileInputStream fin = new FileInputStream(file);
				DataInputStream din = new DataInputStream(fin);
				ObjectInputStream objin=new ObjectInputStream(fin);
				//
				flag_touch = din.readBoolean();
				flag_click = din.readBoolean();
				volume = din.readInt();
				transparence = din.readInt();
				playModel = din.readInt();
				skinpic=(String)objin.readObject();
				//
				objin.close();
				din.close();
				fin.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			return;
		}
	}

	public void initList() {
		File file = new File("E:\\113Player\\save\\list.rgy");
		if (file.exists()) {
			try {
				FileInputStream fin = new FileInputStream(file);
				ObjectInputStream objin = new ObjectInputStream(fin);
				music_count = Integer.parseInt((String) objin.readObject());
				for (int index = 0; index < music_count; index++) {
					String str = (String) objin.readObject();
					listModel.addElement(str);
				}
				//
				objin.close();
				fin.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			return;
		}
	}

	public void saveSet() {
		try {
			FileOutputStream fout = new FileOutputStream(
					"E:\\113Player\\save\\set.rgy");
			DataOutputStream dout = new DataOutputStream(fout);
			ObjectOutputStream objout=new ObjectOutputStream(fout);
			//
			dout.writeBoolean(flag_touch);
			dout.writeBoolean(flag_click);
			//
			int yinliang = vDialog.slider.getValue();
			dout.writeInt(yinliang);
			//
			int toumingdu = boxDialog.slider_tp.getValue();
			dout.writeInt(toumingdu);
			//
			dout.writeInt(playModel);
			//對象字節流
			String str_bgpic=background.getIcon().toString();
			objout.writeObject(str_bgpic);
			//
			objout.close();
			dout.close();
			fout.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void saveList() {
		try {
			FileOutputStream fout = new FileOutputStream(
					"E:\\113Player\\save\\list.rgy");
			ObjectOutputStream objout = new ObjectOutputStream(fout);
			String str_mcount = "" + music_count;
			objout.writeObject(str_mcount);
			for (int index = 0; index < music_count; index++) {
				String str = listModel.getElementAt(index);
				objout.writeObject(str);
			}
			objout.close();
			fout.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void createInterface() {
		// 去掉窗口的裝飾
		this.setUndecorated(true);
		// 改變窗體上的圖標,換成自己的圖標
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image img = kit.getImage("img/TT.jpg");
		setIconImage(img);
		// 不使用佈局
		this.setLayout(null);
		// 關閉與最小化
		ImageIcon imageIcon1 = new ImageIcon("img/close3.png");
		label_close = new JLabel(imageIcon1);
		label_close.setBounds(0, 0, imageIcon1.getIconWidth(),
				imageIcon1.getIconHeight());
		this.add(label_close);
		//
		ImageIcon imageIcon2 = new ImageIcon("img/mini3.png");
		label_mini = new JLabel(imageIcon2);
		label_mini.setBounds(35, 0, imageIcon2.getIconWidth(),
				imageIcon2.getIconHeight());
		this.add(label_mini);
		// 歌曲列表
		this.listModel = new DefaultListModel<String>();
		this.list = new JList<String>(this.listModel);
		JScrollPane sp = new JScrollPane(this.list);
		sp.setBounds(5, 40, 200, 350);
		this.list.setBackground(Color.ORANGE);
		this.list.setFont(new Font("微軟雅黑", 0, 13));
		this.add(sp);
		// 快捷菜單
		popupmenu = new JPopupMenu();
		String menuitemstr[] = { "播放", "刪除", "隨機播放", "順序播放", "單曲循環", "列表循環" };
		popmenuitem = new JMenuItem[menuitemstr.length];
		for (int i = 0; i < popmenuitem.length; i++) {
			popmenuitem[i] = new JMenuItem(menuitemstr[i]);// 菜單項
			popmenuitem[i].setFont(new Font("楷體", 0, 14));
			popupmenu.add(popmenuitem[i]);// 快捷菜單加入菜單項
		}
		this.list.add(popupmenu);
		// 登錄與註冊
		ImageIcon imageIcon3 = new ImageIcon("img/login.png");
		label_login = new JLabel(imageIcon3);
		label_login.setBounds(this.getWidth() - 122, 0, 61, 30);
		this.add(label_login);
		ImageIcon imageIcon4 = new ImageIcon("img/register.png");
		label_register = new JLabel(imageIcon4);
		label_register.setBounds(this.getWidth() - 61, 0, 61, 30);
		this.add(label_register);
		// 歌曲搜索(連接服務器搜索)
		ImageIcon imageIcon5 = new ImageIcon("img/search.png");
		label_search = new JLabel(imageIcon5);
		label_search.setBounds(270, 0, 30, 30);
		this.add(label_search);
		//
		tf_search = new JTextField("請輸入歌曲名");
		tf_search.setBounds(305, 3, 200, 27);
		this.add(tf_search);
		// logo
		ImageIcon imageIcon6 = new ImageIcon("img/logo.gif");
		JLabel label_logo = new JLabel(imageIcon6);
		label_logo.setBounds(230, 0, 30, 30);
		this.add(label_logo);
		// 113悅音 “樂”在其中
		ImageIcon imageIcon7 = new ImageIcon("img/yueyin.png");
		JLabel label_yueyin = new JLabel(imageIcon7);
		label_yueyin.setBounds(230, 35, 400, 50);
		this.add(label_yueyin);
		// 用戶名的label
		label_username = new JLabel("未登錄");
		label_username.setFont(new Font("楷體", 1, 17));
		label_username.setBounds(100, 0, 100, 30);
		label_username.setHorizontalAlignment(JLabel.RIGHT);
		label_username.setForeground(Color.BLUE);
		this.add(label_username);
		// 隨便聽聽,音樂下載,本地音樂
		ImageIcon imageIcon8 = new ImageIcon("img/free.png");
		label_free = new JLabel(imageIcon8);
		label_free.setBounds(230, 100, 110, 142);
		this.add(label_free);
		//
		ImageIcon imageIcon9 = new ImageIcon("img/download.png");
		label_download = new JLabel(imageIcon9);
		label_download.setBounds(360, 100, 110, 142);
		this.add(label_download);
		//
		ImageIcon imageIcon10 = new ImageIcon("img/local.png");
		label_local = new JLabel(imageIcon10);
		label_local.setBounds(490, 100, 110, 142);
		this.add(label_local);
		// 歌曲進度條
		panel_pbar = new JPanel();
		panel_pbar.setBounds(225, 250, 380, 40);
		panel_pbar.setOpaque(false);
		panel_pbar.setLayout(null);
		this.add(panel_pbar);
		slider_progress = new JSlider(JSlider.HORIZONTAL, 0, 1000, 0);
		slider_progress.setBounds(5, 10, 380, 30);
		slider_progress.setUI(new MySliderUI());
		slider_progress.setOpaque(false);
		panel_pbar.add(slider_progress);
		// 快進
		ImageIcon imageIcon17 = new ImageIcon("img/fast.png");
		label_fast = new JLabel(imageIcon17);
		label_fast.setBounds(580, 315, 25, 25);
		this.add(label_fast);
		// 快退
		ImageIcon imageIcon18 = new ImageIcon("img/slow.png");
		label_slow = new JLabel(imageIcon18);
		label_slow.setBounds(230, 315, 25, 25);
		this.add(label_slow);
		// 當前時間標誌
		label_currenttime = new JLabel("00:00");
		label_currenttime.setBounds(230, 290, 40, 20);
		this.add(label_currenttime);
		// 歌曲總時間標誌
		label_alltime = new JLabel("00:00");
		label_alltime.setBounds(580, 290, 40, 20);
		this.add(label_alltime);
		// 歌曲控制按鈕
		ImageIcon imageIcon11 = new ImageIcon("img/start.png");
		label_startAndPause = new JLabel(imageIcon11);
		label_startAndPause.setBounds(390, 300, 50, 50);
		this.add(label_startAndPause);
		//
		ImageIcon imageIcon12 = new ImageIcon("img/former.png");
		label_former = new JLabel(imageIcon12);
		label_former.setBounds(340, 305, 40, 40);
		this.add(label_former);
		//
		ImageIcon imageIcon13 = new ImageIcon("img/next.png");
		label_next = new JLabel(imageIcon13);
		label_next.setBounds(450, 305, 40, 40);
		this.add(label_next);
		//
		ImageIcon imageIcon14 = new ImageIcon("img/volume.png");
		label_volume = new JLabel(imageIcon14);
		label_volume.setBounds(500, 307, 40, 40);
		this.add(label_volume);
		// 音量滑塊對話框
		vDialog = new VolumeDialog(this, volume);
		vDialog.setVisible(false);
		// 音效開關 (OFF/ON)
		ImageIcon imageIcon15 = new ImageIcon("img/OFF.png");
		label_OFFandON = new JLabel(imageIcon15);
		label_OFFandON.setBounds(575, 375, 60, 24);
		this.add(label_OFFandON);
		// 悅音軒
		ImageIcon imageIcon16 = new ImageIcon("img/yueyinxuan.png");
		label_yueyinBox = new JLabel(imageIcon16);
		label_yueyinBox.setBounds(230, 350, 114, 40);
		this.add(label_yueyinBox);
		// 悅音軒(包括透明度設置)
		boxDialog = new BoxDialog(MainFrame2.this, transparence);
		boxDialog.setVisible(false);
		// 心情驛站
		moodBoxDialog = new MoodBoxDialog(boxDialog);
		moodBoxDialog.setVisible(false);
		// 微窗
		smallFrame = new SmallFrame(this);
		smallFrame.setVisible(false);
		// 便籤
		notepadDialog = new NotepadDialog(this);
		notepadDialog.setVisible(false);
		//皮膚
		skinBoxDialog=new SkinBoxDialog(this);
		skinBoxDialog.setVisible(false);
		//登錄框
		loginDialog=new LoginDialog(this);
		loginDialog.setVisible(false);
		//註冊框
		registerDialog=new RegisterDialog(this);
		registerDialog.setVisible(false);
		//換皮膚
		if (skinpic != null) {
			ImageIcon image = new ImageIcon(skinpic);
			this.background.setIcon(image);
			this.moodBoxDialog.background.setIcon(image);
		}
		//
		this.setVisible(true);
	}

	public void registerEvent() {
		// 拖動窗口
		this.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {
				int xOnScreen = e.getXOnScreen();
				int yOnScreen = e.getYOnScreen();
				int xx = xOnScreen - xOld;
				int yy = yOnScreen - yOld;
				MainFrame2.this.setLocation(xx, yy);
				vDialog.setLocation(MainFrame2.this.getX() + 635,
						MainFrame2.this.getY() + 250);
				boxDialog.setLocation(MainFrame2.this.getX() + 205,
						MainFrame2.this.getY() + 400);
				moodBoxDialog.setLocation(MainFrame2.this.getX() + 635,
						MainFrame2.this.getY());
				notepadDialog.setLocation(MainFrame2.this.getX() + 635,
						MainFrame2.this.getY() + 200);
				skinBoxDialog.setLocation(MainFrame2.this.getX() + 230,
						MainFrame2.this.getY() + 100);
			}
		});
		this.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				xOld = e.getX();
				yOld = e.getY();
			}
		});
		// 點擊兩次關閉程序
		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1
						&& e.getClickCount() == 2) {
					File file = new File("sound/click.wav");
					PlayMusic.play(file, flag_click);
					// 保存(設置與歌曲列表)
					saveSet();
					saveList();
					//
					System.exit(0);
				}
			}
		});
		/**
		 * 關閉與最小化按鈕,併爲其添加事件
		 */
		label_close.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/close.png");
				label_close.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/close3.png");
				label_close.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/close2.png");
				label_close.setIcon(icon);
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
			}

			public void mouseReleased(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/close.png");
				label_close.setIcon(icon);
				// 保存(設置與歌曲列表)
				saveSet();
				saveList();
				//
				System.exit(0);
			}
		});
		// 最小化
		label_mini.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/mini.png");
				label_mini.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/mini3.png");
				label_mini.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/mini2.png");
				label_mini.setIcon(icon);
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
			}

			// 最小化
			public void mouseReleased(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/mini.png");
				label_mini.setIcon(icon);
				MainFrame2.this.setState(Frame.ICONIFIED);
				// 便籤不可見,便籤開關關閉
				ImageIcon icon1 = new ImageIcon("img/OFF.png");
				label_OFFandON.setIcon(icon1);
				MainFrame2.this.notepadDialog.setVisible(false);
				// 悅音軒關閉
				MainFrame2.this.boxDialog.setVisible(false);
				// 音量面板關閉
				vDialog.setVisible(false);
			}
		});
		/**
		 * 爲登錄註冊添加效果響應事件
		 */
		label_login.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/login1.png");
				label_login.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/login.png");
				label_login.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				//
				loginDialog.setVisible(true);
			}
		});
		label_register.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/register1.png");
				label_register.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/register.png");
				label_register.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				//
				registerDialog.setVisible(true);
			}
		});
		/**
		 * 搜索
		 */
		label_search.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/search1.png");
				label_search.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/search.png");
				label_search.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				// 補充事件監聽
				new ClientSearchThread(MainFrame2.this).start();
			}
		});
		tf_search.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				tf_search.setText("");
			}
		});
		tf_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 補充事件監聽
				new ClientSearchThread(MainFrame2.this).start();
			}
		});
		/**
		 * 隨便聽聽
		 */
		label_free.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/free2.png");
				label_free.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/free.png");
				label_free.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File sound = new File("sound/click.wav");
				PlayMusic.play(sound, flag_click);
				// 補充事件監聽
				playModel = model_random;// 本地音樂隨機播放
				//
				if (player != null) {
					player.stop();
					player.setMediaTime(new Time(0));
				}
				//
				Random r = new Random();
				int index = r.nextInt(music_count);
				list.setSelectedIndex(index);// 隨機一曲
				//
				String musicPath = list.getSelectedValue();
				File file = new File(musicPath);
				if (file.exists()) {
					musicGo(musicPath);
					controlLis();
					showAllTime();
					//
					if (th == null) {
						th = new UpdateProgressThread();
						th.start();
					}
				} else {
					new TipDialog(MainFrame2.this, "文件路徑錯誤,請刪除該記錄", TipDialog.TIP_ERROR);
					File errorSound = new File("sound/error.wav");
					PlayMusic.play(errorSound, true);
				}
				System.out.println("隨便聽聽");
			}
		});
		/**
		 * 音樂下載
		 */
		label_download.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/download2.png");
				label_download.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/download.png");
				label_download.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				// 補充事件監聽
				new ClientDownLoadThread(MainFrame2.this).start();
			}
		});
		/**
		 * 本地音樂
		 */
		label_local.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/local2.png");
				label_local.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/local.png");
				label_local.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				ImageIcon icon = new ImageIcon("img/pause.png");
				label_startAndPause.setIcon(icon);
				// 補充事件監聽
				String currentfile, selectfile, currentDirectory = null;
				FileDialog fd = new FileDialog(MainFrame2.this, "打開媒體文件",
						FileDialog.LOAD);
				fd.setDirectory(currentDirectory);
				fd.setVisible(true);
				if (fd.getFile() == null) {
					return;
				}
				selectfile = fd.getFile();
				currentDirectory = fd.getDirectory();
				currentfile = currentDirectory + selectfile;
				String musicPath = currentfile;
				//
				for(int i=0;i<listModel.getSize();i++){
					if(listModel.getElementAt(i).equals(musicPath)){
						new TipDialog(MainFrame2.this,"歌曲已存在!",TipDialog.TIP_ERROR);
						File sound=new File("sound/error.wav");
						PlayMusic.play(sound,true);
						return;
					}
				}
				//
				listModel.addElement(musicPath);// 加入歌曲列表
				list.setSelectedIndex(music_count);
				music_count++;
				if (player != null) {
					player.stop();
					player.setMediaTime(new Time(0));
				}
				musicGo(musicPath);
				controlLis();
				player.start();
				showAllTime();
				if (gc == null) {
					gc = player.getGainControl();
				}
				//
				if (th == null) {
					th = new UpdateProgressThread();
					th.start();
				}
				//
			}
		});
		/**
		 * 音樂控制按鈕:開始,下一曲,上一曲
		 */
		label_startAndPause.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				if (label_startAndPause.getIcon().toString()
						.equals("img/start.png")) {
					ImageIcon icon = new ImageIcon("img/start1.png");
					label_startAndPause.setIcon(icon);
				} else {
					ImageIcon icon = new ImageIcon("img/pause1.png");
					label_startAndPause.setIcon(icon);
				}
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				if (label_startAndPause.getIcon().toString()
						.equals("img/start1.png")) {
					ImageIcon icon = new ImageIcon("img/start.png");
					label_startAndPause.setIcon(icon);
				} else {
					ImageIcon icon = new ImageIcon("img/pause.png");
					label_startAndPause.setIcon(icon);
				}
			}

			public void mousePressed(MouseEvent e) {
				if (label_startAndPause.getIcon().toString()
						.equals("img/start1.png")) {
					ImageIcon icon = new ImageIcon("img/pause1.png");
					label_startAndPause.setIcon(icon);
					// 補充事件監聽
					player.start();

				} else {
					ImageIcon icon = new ImageIcon("img/start1.png");
					label_startAndPause.setIcon(icon);
					// 補充事件監聽
					player.stop();
				}
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
			}
		});
		/**
		 * 上一曲
		 */
		label_former.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/former1.png");
				label_former.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/former.png");
				label_former.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File sound = new File("sound/click.wav");
				PlayMusic.play(sound, flag_click);
				// 補充事件監聽
				int index = MainFrame2.this.list.getSelectedIndex();
				if (index > 0) {
					MainFrame2.this.list.setSelectedIndex(index - 1);// 上一曲
					// 播放
					if (player != null) {
						player.stop();
						player.setMediaTime(new Time(0));
					}
					String musicPath = list.getSelectedValue();
					File file = new File(musicPath);
					if (file.exists()) {
						musicGo(musicPath);
						controlLis();
						showAllTime();
						//
						if (th == null) {
							th = new UpdateProgressThread();
							th.start();
						}
					} else {
						new TipDialog(MainFrame2.this, "文件路徑錯誤,請刪除該記錄", TipDialog.TIP_ERROR);
						File errorSound = new File("sound/error.wav");
						PlayMusic.play(errorSound, true);
					}
				}
			}
		});
		/**
		 * 下一曲
		 */
		label_next.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/next1.png");
				label_next.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/next.png");
				label_next.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File sound = new File("sound/click.wav");
				PlayMusic.play(sound, flag_click);
				// 補充事件監聽
				int index = MainFrame2.this.list.getSelectedIndex();
				if (index < music_count) {
					MainFrame2.this.list.setSelectedIndex(index + 1);// 下一曲
					// 播放
					if (player != null) {
						player.stop();
						player.setMediaTime(new Time(0));
					}
					String musicPath = list.getSelectedValue();
					File file = new File(musicPath);
					if (file.exists()) {
						musicGo(musicPath);
						controlLis();
						showAllTime();
						//
						if (th == null) {
							th = new UpdateProgressThread();
							th.start();
						}
					} else {
						new TipDialog(MainFrame2.this, "文件路徑錯誤,請刪除該記錄", TipDialog.TIP_ERROR);
						File errorSound = new File("sound/error.wav");
						PlayMusic.play(errorSound, true);
					}
				}
			}
		});
		/**
		 * 快進
		 */
		label_fast.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
				//
				ImageIcon icon = new ImageIcon("img/fast1.png");
				label_fast.setIcon(icon);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/fast.png");
				label_fast.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				//
				second_current = player.getMediaTime().getSeconds();
				if (second_current < second_all - 5) {
					second_current = second_current + 5;// 前進5秒
					player.setMediaTime(new Time(second_current));
				}
				String currenttime = (int) (second_current / 60) + ":"
						+ (int) second_current % 60;
				label_currenttime.setText(currenttime);
				double rate = second_current / second_all;
				progress = (int) (rate * 1000);
				slider_progress.setValue(progress);
			}
		});
		/**
		 * 快退
		 */
		label_slow.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
				//
				ImageIcon icon = new ImageIcon("img/slow1.png");
				label_slow.setIcon(icon);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/slow.png");
				label_slow.setIcon(icon);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				//
				second_current = player.getMediaTime().getSeconds();
				if (second_current > 5) {
					second_current = second_current - 5;// 後退5秒
					player.setMediaTime(new Time(second_current));
				}
				String currenttime = (int) (second_current / 60) + ":"
						+ (int) second_current % 60;
				label_currenttime.setText(currenttime);
				double rate = second_current / second_all;
				progress = (int) (rate * 1000);
				slider_progress.setValue(progress);
			}
		});
		/**
		 * 音量
		 */
		label_volume.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				if (label_volume.getIcon().toString().equals("img/volume.png")) {
					ImageIcon icon = new ImageIcon("img/volume1.png");
					label_volume.setIcon(icon);
				}
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
				vDialog.setLocation(MainFrame2.this.getX() + 635,
						MainFrame2.this.getY() + 250);
				vDialog.setVisible(true);
			}

			public void mouseExited(MouseEvent e) {
				if (label_volume.getIcon().toString().equals("img/volume1.png")) {
					ImageIcon icon = new ImageIcon("img/volume.png");
					label_volume.setIcon(icon);
				} else {
					ImageIcon icon = new ImageIcon("img/volume2.png");
					label_volume.setIcon(icon);
				}
			}

			public void mousePressed(MouseEvent e) {
				if (label_volume.getIcon().toString().equals("img/volume1.png")) {
					ImageIcon icon = new ImageIcon("img/volume2.png");
					label_volume.setIcon(icon);
					vDialog.slider.setValue(0);
				} else {
					ImageIcon icon = new ImageIcon("img/volume1.png");
					label_volume.setIcon(icon);
					vDialog.slider.setValue(70);
				}
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
			}
		});
		/**
		 * 音量對話框消失
		 */
		vDialog.slider.addMouseListener(new MouseAdapter() {
			public void mouseExited(MouseEvent e) {
				vDialog.setVisible(false);
			}
		});
		/**
		 * 小開關(便籤)
		 */
		label_OFFandON.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (label_OFFandON.getIcon().toString().equals("img/OFF.png")) {
					ImageIcon icon = new ImageIcon("img/ON.png");
					label_OFFandON.setIcon(icon);
					MainFrame2.this.notepadDialog.setVisible(true);
				} else {
					ImageIcon icon = new ImageIcon("img/OFF.png");
					label_OFFandON.setIcon(icon);
					MainFrame2.this.notepadDialog.setVisible(false);
				}
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
			}
		});
		/**
		 * 悅音軒
		 */
		label_yueyinBox.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/yueyinxuan1.png");
				label_yueyinBox.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mouseExited(MouseEvent e) {
				ImageIcon icon = new ImageIcon("img/yueyinxuan.png");
				label_yueyinBox.setIcon(icon);
				File file = new File("sound/touch.WAV");
				PlayMusic.play(file, flag_touch);
			}

			public void mousePressed(MouseEvent e) {
				File file = new File("sound/click.wav");
				PlayMusic.play(file, flag_click);
				//
				if (isSuccess == true) {
					if (switchControl == false) {
						boxDialog.setVisible(true);
						switchControl = true;
					} else {
						boxDialog.setVisible(false);
						switchControl = false;
					}
				}
			}
		});
		/**
		 * 音量調節
		 */
		vDialog.slider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				volume = vDialog.slider.getValue();
				float vol = (float) volume / 100;
				if (gc != null) {
					gc = player.getGainControl();
					gc.setLevel(vol);
				}
			}
		});
		/**
		 * 歌曲列表
		 */
		list.addMouseListener(new MouseAdapter() {// 鼠標事件處理方法,右擊彈出菜單
			public void mouseClicked(MouseEvent e) {
				if (e.getModifiers() == MouseEvent.BUTTON3_MASK) // 單擊的是鼠標右鍵
					popupmenu.show(list, e.getX(), e.getY()); // 在鼠標單擊處顯示快捷菜單
				if (e.getClickCount() == 2) { // 雙擊事件
					ImageIcon icon = new ImageIcon("img/pause.png");
					label_startAndPause.setIcon(icon);
					if (player != null) {
						player.stop();
						player.setMediaTime(new Time(0));
					}
					String musicPath = list.getSelectedValue();
					File file = new File(musicPath);
					if (file.exists()) {
						musicGo(musicPath);
						controlLis();
						showAllTime();
						//
						if (th == null) {
							th = new UpdateProgressThread();
							th.start();
						}
					} else {
						new TipDialog(MainFrame2.this, "文件路徑錯誤,請刪除該記錄", TipDialog.TIP_ERROR);
						File errorSound = new File("sound/error.wav");
						PlayMusic.play(errorSound, true);
					}
				}
			}
		});
		/**
		 * 快捷菜單
		 */
		popmenuitem[0].addActionListener(new ActionListener() {// 播放
					@Override
					public void actionPerformed(ActionEvent e) {
						ImageIcon icon = new ImageIcon("img/pause.png");
						label_startAndPause.setIcon(icon);
						if (player != null) {
							player.stop();
							player.setMediaTime(new Time(0));
						}
						String musicPath = list.getSelectedValue();
						File file = new File(musicPath);
						if (file.exists()) {
							musicGo(musicPath);
							controlLis();
							showAllTime();
							//
							if (th == null) {
								th = new UpdateProgressThread();
								th.start();
							}
						} else {
							new TipDialog(MainFrame2.this, "文件路徑錯誤,請刪除該記錄", TipDialog.TIP_ERROR);
							File errorSound = new File("sound/error.wav");
							PlayMusic.play(errorSound, true);
						}
					}
				});

		popmenuitem[1].addActionListener(new ActionListener() {// 刪除記錄
					@Override
					public void actionPerformed(ActionEvent e) {
						int index = list.getSelectedIndex();
						listModel.removeElementAt(index);
						if (player != null) {
							player.stop();
							player.setMediaTime(new Time(0));
						}
						music_count--;
					}
				});

		popmenuitem[2].addActionListener(new ActionListener() {// 隨機播放
					@Override
					public void actionPerformed(ActionEvent e) {
						playModel = model_random;
					}
				});
		popmenuitem[3].addActionListener(new ActionListener() {// 順序播放
					@Override
					public void actionPerformed(ActionEvent e) {
						playModel = model_order;
					}
				});
		popmenuitem[4].addActionListener(new ActionListener() {// 單曲循環
					@Override
					public void actionPerformed(ActionEvent e) {
						playModel = model_onecircle;
					}
				});
		popmenuitem[5].addActionListener(new ActionListener() {// 列表循環
					@Override
					public void actionPerformed(ActionEvent e) {
						playModel = model_listcircle;
					}
				});
	}

	/**
	 * 初始化player,併爲其添加監聽事件
	 * 
	 * @param musicPath
	 */
	public void musicGo(String musicPath) {
		File musicFile = new File(musicPath);
		MediaLocator locator = new MediaLocator("file:"
				+ musicFile.getAbsolutePath());
		try {
			player = Manager.createRealizedPlayer(locator);
			player.prefetch();// 預讀文件
		} catch (Exception ex) {
			if(ex instanceof CannotRealizeException){
				new TipDialog(this, "無法識別該樂曲", TipDialog.TIP_ERROR);
				File sound=new File("sound/error.wav");
				PlayMusic.play(sound, true);
			}
		}
	}

	/**
	 * 爲player的控制加監聽
	 */
	public void controlLis() {
		// 必須初始化之後纔可以加監聽
		if (player != null) {
			player.addControllerListener(new ControllerListener() {
				@Override
				public void controllerUpdate(ControllerEvent e) {
					// 調節音量
					gc = player.getGainControl();
					volume = vDialog.slider.getValue();
					float vol = (float) volume / 100;
					gc.setLevel(vol);
					//
					if (e instanceof PrefetchCompleteEvent) {// 預讀完成執行
						player.start();
						return;
					}
					if (e instanceof EndOfMediaEvent) {// 一曲目結束執行
						int index;
						String musicPath;
						switch (playModel) {
						case model_random:
							player.setMediaTime(new Time(0));
							//
							Random r = new Random();
							index = r.nextInt(music_count);
							list.setSelectedIndex(index);// 隨機一曲
							musicPath = list.getSelectedValue();
							musicGo(musicPath);
							controlLis();
							showAllTime();
							//
							System.out.println("隨機模式");
							break;
						case model_order:
							System.out.println("順序播放");
							player.setMediaTime(new Time(0));
							//
							index = list.getSelectedIndex();
							if (index < music_count - 1) {
								list.setSelectedIndex(index + 1);// 下一曲
								musicPath = list.getSelectedValue();
								musicGo(musicPath);
								controlLis();
								showAllTime();
							}
							break;
						case model_onecircle:
							player.setMediaTime(new Time(0));
							player.start();
							//
							showAllTime();
							//
							System.out.println("單曲循環模式");
							break;
						case model_listcircle:
							System.out.println("列表循環");
							player.setMediaTime(new Time(0));
							//
							index = list.getSelectedIndex();
							if (index < music_count - 1) {
								list.setSelectedIndex(index + 1);// 下一曲
								musicPath = list.getSelectedValue();
								musicGo(musicPath);
								controlLis();
								showAllTime();
							} else {
								System.out.println("回到第一首");
								list.setSelectedIndex(0);// 回到第一首
								musicPath = list.getSelectedValue();
								musicGo(musicPath);
								controlLis();
								showAllTime();
							}
							break;
						default:
							break;
						}
					}
				}
			});
		}else{
			return;
		}
	}

	/**
	 * 顯示該首歌曲總時長
	 */
	public void showAllTime() {
		if(player!=null){
			second_all = player.getDuration().getSeconds();
			String alltime = (int) (second_all / 60) + ":" + (int) second_all % 60;
			label_alltime.setText(alltime);
		}else {
			return;
		}
	}

	class UpdateProgressThread extends Thread {

		public void run() {
			while (true) {
				if (player != null) {
					second_current = player.getMediaTime().getSeconds();
					String currenttime = (int) (second_current / 60) + ":"
							+ (int) second_current % 60;
					label_currenttime.setText(currenttime);
					double rate = second_current / second_all;
					progress = (int) (rate * 1000);
					slider_progress.setValue(progress);
					try {
						Thread.sleep(50);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		new MainFrame2("113悅音");
	}

}

貼點效果圖吧,感覺直觀些…………










工程代碼:

http://download.csdn.net/detail/u011700203/8659411


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章