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

📄 musicplayer.java

📁 java写的一个播放器,能够播放mp3,带源码程序,有用的朋友可以看看.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.sun.xml.internal.fastinfoset.sax.Properties;

public class MusicPlayer extends JFrame {

	// private static MusicPlayer myPlayer = new MusicPlayer();
	private Container contentPane = this.getContentPane();

	private JMenuBar menubar = new JMenuBar();

	private JMenu jmenu_file, jmenu_paly, jmenu_help;// 主菜单

	private JMenu menuItem_sound, Menu_view, menu_control, menu_playerMode,
			menuItem_controlPlayer;// 复选框菜单

	// 主菜单菜单条
	private MyMenuItem menuItem_open, menuItem_exit, menuItem_palyOrPause,
			menuItem_stop, menuItem_help, menuItem_volumeIncrease,
			menuItem_volumeDecrease, menuItem_about, menuItem_rewind,
			menuItem_skinForwind;

	private MyRadioButtonMenuItem menuItem_onlyOneMode, menuItem_forOneMode,
			menuItem_turnMode, menuItem_randomMode, menuItem_onTop,
			menuItem_AutoChange, menuItem_noSound;

	// 复选框菜单条

	// 右键菜单条
	private MyMenuItem menuItem_paly_Popup, menuItem_stop_Popup,
			menuItem_skinForwind_Popup, menuItem_rewind_Popup,
			menuItem_Addfile, menuItem_allScreen, menuItem_delete,
			menuItem_deleteAll, menuItem_playSelect;

	// but_last-上一曲,but_pla-播放,but_inte-暂停,but_stop停止,but_next下一曲,but_speed快进,but_recede快退
	private MyButton but_rewind, but_play, but_stop, but_fastForward,
			but_sounds, but_skipBackward, but_skipForward, but_pause,
			but_music;

	private Vector<String> fileName = new Vector<String>();// musicList-用于装列表中的元素

	private Vector<File> fileDirection = new Vector<File>();

	private JSlider slider_time = new JSlider();// slider_time时间进度条

	private ButtonGroup group__playerMode = new ButtonGroup();

	private ButtonGroup group__playerControl = new ButtonGroup();

	private JPopupMenu popupMenuAll, popupMenuList;// 右键弹出菜单

	private JSlider jSlider_sound;// 音量控制杆

	private Player player;// player播放声音对像

	private JSplitPane splitpane;

	private boolean firstPlayer = true, tempPause = true, playerOrPause = true,AllScreenOrOriginal = false;

	/*
	 * panel_view为视频面板.panel_down为整体布局,把窗口分成上下两部分.panel_play为添加播放按钮
	 * panel_control布局为添加进度条,音量控制和其它按钮的面板.panel_prograBar为添加进度条的面板.
	 * panel_button为添加按钮的面板
	 */

	private JPanel panel_view, panel_down, panel_play, panel_control,
			panel_prograBar, panel_button, panel_AddButton, panel_Addsounds;

	private MyList list, popupList;// 播放列表

	private Component visual;

	static double newTime, fileTime;

	static int width = 530, height = 380, x, y;

	static Thread timeThread;

	private File[] files;

	private JPopupMenu popup_menu = new JPopupMenu();

	private File fileDir;

	private JLabel lable_infor, label_view;

	private JScrollPane scrollPopup;

	ResultSet rs;

	File userInfro = new File("user.dat");

	private Time pauseTime;

	MenuItem exitItem_playOrPause_SystemTray = new MenuItem("播放/暂停");

	MenuItem exitItem_stop_SystemTray = new MenuItem("停止");

	public MusicPlayer() {
		super();
		this.setSize(width, height);
		this.setLocationRelativeTo(this);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.add(menubar, BorderLayout.NORTH);
		this.setMinimumSize(new Dimension(530, 120));
		this.setTitle("java播放器--淘气一族");
		this.initUI();
	}

	private void initUI() {

		changWindowIcon();
		initPanel();
		initButton();
		initSetComponentInPanel();
		initMenu();
		initPopupMenu();
		initAddMenu();
		getSplitPane();
		initAddListenerInMenu();

		initAddButton();
		initAddListenerInButton();

		initAddListenerInPopupMenu();
		initButtonChangFaceListener();
		initAddAllCompents();
		initSetButton();
		controlSliderTime();
		initMusicModeControl();
		setAcceleratorInMenuItem();
		initSystemTray();
		readFileToList();
		AutoAddFileToList();

	}

	private void initPanel() {
		panel_down = new JPanel();

		panel_play = new JPanel();

		panel_control = new JPanel();

		panel_prograBar = new JPanel();

		panel_button = new JPanel();

		panel_AddButton = new JPanel();

		panel_Addsounds = new JPanel();

	}

	// 音乐模式下的按钮处理
	public void initMusicModeControl() {
		but_music.addMouseListener(new MouseAdapter() {

			MyMenu popup_menuList = new MyMenu("播放曲目");

			public void mouseClicked(MouseEvent e) {
				int mod = e.getModifiers() & 4;
				if (e.getButton() == MouseEvent.BUTTON1
						&& e.getClickCount() == 2) {
					int index = list.getSelectedIndex();
					if (but_music.getActionCommand() == "true") {
						changToMusicMode();
					} else {
						changToMoveMode(index);
					}
				} else if (mod != 0) {
					System.out.println("Ok");

					popup_menu.add(popup_menuList);
					popup_menu.show(but_music, e.getX(), e.getY());
					popupList.setSelectedIndex(list.getSelectedIndex());
					popup_menuList.add(popupList);
				}
			}
		});
	}

	private void initSetComponentInPanel() {

		JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		panel_AddButton.addMouseListener(new RightMouseListener());

		panel_down.setLayout(new BorderLayout());
		panel_control.setLayout(new BorderLayout());

		panel_prograBar.setLayout(new BorderLayout());

		slider_time.setValue(0);
		slider_time.setPreferredSize(new Dimension(this.getX(), 15));

		panel_button.setLayout(new BorderLayout());
		panel_AddButton.setLayout(new BoxLayout(panel_AddButton,
				BoxLayout.X_AXIS));

		lable_infor = new JLabel("音量:100 进度:0:0:0");
		TitledBorder titleBorder = new TitledBorder("播放信息");
		titleBorder.setTitleColor(new Color(51, 105, 186));
		titleBorder.setTitleFont(new Font("宋体", 6, 11));
		lable_infor.setBorder(titleBorder);
		lable_infor.setForeground(new Color(51, 105, 186));
		lable_infor.setFont(new Font("宋体", 6, 11));
		lable_infor.setPreferredSize(new Dimension(140, 35));
		panel_Addsounds.add(lable_infor, BorderLayout.CENTER);
		panel_Addsounds.add(but_music);

	}

	private void initMenu() {
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		jmenu_file = new MyMenu("文件");
		jmenu_paly = new MyMenu("播放");

		Menu_view = new MyMenu("显示");
		jmenu_help = new MyMenu("帮助");

		menuItem_onTop = new MyRadioButtonMenuItem("前端显示");

		menuItem_sound = new MyMenu("音量");

		menuItem_open = new MyMenuItem("打开文件");
		menuItem_exit = new MyMenuItem("退出");

		menuItem_palyOrPause = new MyMenuItem("播放/暂停");
		menuItem_stop = new MyMenuItem("停止");

		menu_control = new MyMenu("播放控制");
		menu_playerMode = new MyMenu("播放列表");

		menuItem_skinForwind = new MyMenuItem("快进");
		menuItem_rewind = new MyMenuItem("快退");
		menuItem_skinForwind_Popup = new MyMenuItem("快进");
		menuItem_rewind_Popup = new MyMenuItem("快退");

		menuItem_AutoChange = new MyRadioButtonMenuItem("自动切换", true);
		menuItem_about = new MyMenuItem("关于");
		menuItem_help = new MyMenuItem("帮助");

		menuItem_volumeIncrease = new MyMenuItem("增大");
		menuItem_volumeDecrease = new MyMenuItem("减小");
		menuItem_noSound = new MyRadioButtonMenuItem("静音");

		menuItem_onlyOneMode = new MyRadioButtonMenuItem("单曲播放");
		menuItem_forOneMode = new MyRadioButtonMenuItem("单曲循环");
		menuItem_turnMode = new MyRadioButtonMenuItem("循环播放");
		menuItem_randomMode = new MyRadioButtonMenuItem("随机播放", true);

	}

	private void initAddMenu() {
		// 添加主菜单到菜单栏
		menubar.add(jmenu_file);
		menubar.add(jmenu_paly);
		menubar.add(Menu_view);
		menubar.add(jmenu_help);

		// 添加到主菜单条
		jmenu_file.add(menuItem_open);
		jmenu_file.add(menuItem_exit);
		jmenu_paly.add(menuItem_palyOrPause);
		jmenu_paly.add(menuItem_stop);
		jmenu_paly.add(menu_control);
		menu_control.add(menuItem_skinForwind);
		menu_control.add(menuItem_rewind);

		jmenu_paly.add(menu_playerMode);
		jmenu_paly.add(menuItem_sound);

		Menu_view.add(menuItem_onTop);
		Menu_view.add(menuItem_AutoChange);

		jmenu_help.add(menuItem_about);
		jmenu_help.add(menuItem_help);

		menu_playerMode.add(menuItem_onlyOneMode);
		menu_playerMode.add(menuItem_forOneMode);
		menu_playerMode.add(menuItem_turnMode);
		menu_playerMode.add(menuItem_randomMode);

		menuItem_sound.add(menuItem_volumeIncrease);
		menuItem_sound.add(menuItem_volumeDecrease);
		menuItem_sound.add(menuItem_noSound);

		// 添加到选择菜单框组建上
		group__playerMode.add(menuItem_onlyOneMode);
		group__playerMode.add(menuItem_forOneMode);
		group__playerMode.add(menuItem_turnMode);
		group__playerMode.add(menuItem_randomMode);

		group__playerControl.add(menuItem_skinForwind);
		group__playerControl.add(menuItem_rewind);
	}

	private void initAddListenerInMenu() {
		menuItem_palyOrPause.addActionListener(new CommendActionListener());
		menuItem_paly_Popup.addActionListener(new CommendActionListener());

		menuItem_stop.addActionListener(new CommendActionListener());
		menuItem_stop_Popup.addActionListener(new CommendActionListener());

		menuItem_open.addActionListener(new CommendActionListener());
		menuItem_exit.addActionListener(new CommendActionListener());

		menuItem_stop.addActionListener(new CommendActionListener());

		menuItem_volumeIncrease.addActionListener(new CommendActionListener());
		menuItem_volumeDecrease.addActionListener(new CommendActionListener());
		menuItem_noSound.addActionListener(new CommendActionListener());

		menuItem_skinForwind_Popup
				.addActionListener(new CommendActionListener());

⌨️ 快捷键说明

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