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

📄 musicplayer.java

📁 不要重复上载同一源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		scrollPane.getViewport().add(list);
		return scrollPane;
	}

	private JSlider getSoundControl() {
		jSlider_sound = new JSlider(0, 100, 100);
		jSlider_sound.setToolTipText("音量控制");
		jSlider_sound.addChangeListener(new SoundChangListener());
		jSlider_sound.setPreferredSize(new Dimension(100, 15));
		return jSlider_sound;
	}

	private JTabbedPane getTabbedPane() {
		JTabbedPane tab_list = new JTabbedPane(JTabbedPane.TOP);
		tab_list.addTab("播放列表", getPlayerList());
		return tab_list;
	}

	private void getSplitPane() {
		splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				getTabbedPane_player(), getTabbedPane());
		splitpane.setOneTouchExpandable(true);
		splitpane.setDividerLocation(360);
		splitpane.setDividerSize(6);
		splitpane.setResizeWeight(0.8);

	}

	private JTabbedPane getTabbedPane_player() {
		JTabbedPane tab_player = new JTabbedPane(JTabbedPane.TOP);
		tab_player.addTab("正在播放", getView());
		return tab_player;

	} // 此方法处理一些按键请求

	private class CommendActionListener implements ActionListener {
		boolean tenp = true, temp = true, ifAllScreen = true;

		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if (source == menuItem_open) {
				open();
			} else if (source == menuItem_exit) {
				System.exit(0);
			} else if (source == menuItem_palyOrPause
					|| source == menuItem_paly_Popup
					|| source == exitItem_playOrPause_SystemTray) {
				if (tenp && firstPlayer) {
					playMedia(list.getSelectedIndex());
					tenp = false;
				} else {
					if (player != null) {
						if (tempPause && playerOrPause) {
							System.out.println("pause");
							pauseMedia();
							tempPause = false;
						} else if (!playerOrPause) {
							System.out.println("player");
							pauseToPlayer();
							tempPause = true;
						} else {
							pauseMedia();
						}
					}
				}
			} else if (source == menuItem_stop || source == menuItem_stop_Popup
					|| source == exitItem_stop_SystemTray) {
				stopMedia();
				tenp = true;
			} else if (source == menuItem_skinForwind_Popup
					|| source == menuItem_skinForwind) {

				proControl(5);
			} else if (source == menuItem_rewind
					|| source == menuItem_rewind_Popup) {
				proControl(-5);
			} else if (source == menuItem_onTop) {
				if (menuItem_onTop.isSelected()) {
					setAlwaysOnTop(true);
				} else {
					setAlwaysOnTop(false);
				}
			} else if (source == menuItem_allScreen) {
				System.out.println("allFull");
				if (visual != null) {
					if (ifAllScreen) {
						fullScreenView();
						menuItem_allScreen.setText("退出全屏");
						ifAllScreen = false;
					} else {
						originalView();
						menuItem_allScreen.setText("全屏");
						ifAllScreen = true;
					}
				}
			} else if (source == menuItem_volumeIncrease) {
				changeSounds(10);
			} else if (source == menuItem_volumeDecrease) {
				changeSounds(-10);
			} else if (source == menuItem_noSound) {
				if (menuItem_noSound.isSelected()) {
					but_sounds.setActionCommand("true");
					but_sounds.setIcon(new ImageIcon("img/nosound_.png"));
					mediaMute(but_sounds.getActionCommand());
				} else {
					but_sounds.setActionCommand("false");
					but_sounds.setIcon(new ImageIcon("img/sound.png"));
					mediaMute(but_sounds.getActionCommand());
				}
			} else if (source == menuItem_help) {
				Help help = new Help(null, "帮助中心", true);
				help.setLocationRelativeTo(null);
				help.setVisible(true);
			} else if (source == menuItem_about) {
				About about = new About(null, "关于我们", true);
				about.setLocationRelativeTo(null);
				about.setVisible(true);

			}
		}
	}

	private void changeSounds(int temp) {

		int newSounds = jSlider_sound.getValue() + temp;
		if (newSounds < 0 || newSounds > 100) {
			newSounds = jSlider_sound.getValue();
		}
		but_sounds.setActionCommand("false");
		but_sounds.setIcon(new ImageIcon("img/sound.png"));
		mediaMute(but_sounds.getActionCommand());
		menuItem_noSound.setSelected(false);
		jSlider_sound.setValue(newSounds);
	}

	// 窗口总是在最前端
	public void alwaysOnTop() {
		if (menuItem_onTop.isSelected()) {
			setAlwaysOnTop(true);
		} else {
			setAlwaysOnTop(false);
		}
	}

	// 此方提供为用户更改进度拖动
	public void controlSliderTime() {

		slider_time.addMouseListener(new MouseAdapter() {

			public void mouseReleased(MouseEvent e) {
				if (player != null) {
					newTime = slider_time.getValue();
					player.setMediaTime(new Time(newTime));
					System.out.println("调整文件到: " + newTime);
					System.out.println("-----------------------");
					player.start();
				} else {
					slider_time.setValue(0);
					newTime = 0;
					fileTime = 0;
				}
			}
		});
	}

	// 此类提供为用户更改音量
	private class SoundChangListener implements ChangeListener {

		public void stateChanged(ChangeEvent e) {
			but_sounds.setActionCommand("false");
			but_sounds.setIcon(new ImageIcon("img/sound.png"));
			menuItem_noSound.setSelected(false);
			System.out.println("调节音量大小为: " + (float) jSlider_sound.getValue()
					/ 100);
			if (player != null) {
				player.getGainControl().setLevel(
						(float) jSlider_sound.getValue() / 100);
				System.out.println("音量大小为"
						+ (float) player.getGainControl().getLevel());
				mediaMute(but_sounds.getActionCommand());
			}
			lable_infor.setText("音量:" + jSlider_sound.getValue() + " " + "进度:"
					+ (int) newTime / 3600 + ":" + (int) (newTime % 3600) / 60
					+ ":" + (int) (newTime % 60));
			System.out.println("-----------------------");
			jSlider_sound.setToolTipText(jSlider_sound.getValue() + "");
		}
	}

	// 为播放做准备(还原一些变量和状态)
	public void readyToPlayer() {
		playerOrPause = true;
		panel_play.remove(but_play);
		panel_play.add(but_pause);
		panel_play.updateUI();
		firstPlayer = false;
		newTime = 0;
		fileTime = 0;
	}

	// 播放对象方法
	public void playMedia(int fileIndex) {
		System.out.println(player);
		if (player != null) {
			closePlayerStream();
		}
		readyToPlayer();
		try {
			System.out.println("获取成功:  开始创建播放.................");
			player = Manager.createPlayer(new MediaLocator(getFileToplayer(
					fileIndex).toURI().toURL()));
			player.addControllerListener(new ControlListener());
			System.out.println("添加成功:正在对文件进行预读..............................");
			player.prefetch();

		} catch (NoPlayerException e1) {
			e1.printStackTrace();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		this.setTitle(fileName.elementAt(fileIndex).substring(
				fileName.elementAt(fileIndex).indexOf('.') + 1));
		this.validate();
	}

	// 打开文件后对文件格式进行判断
	private File getFileToplayer(int file) {

		System.out.println("验证播放文件是否存在");

		fileTime = 0;
		if (file != -1) {
			System.out.println("验证文件成功!文件路径 " + fileDirection.get(file));
			fileDir = fileDirection.get(file);

			System.out.println("播放的文件格式: "
					+ fileName.elementAt(file).substring(
							fileName.elementAt(file).lastIndexOf('.') + 1));

			String fileMode = fileName.elementAt(file).substring(
					fileName.elementAt(file).lastIndexOf('.') + 1);
			int index = list.getSelectedIndex();
			if (menuItem_AutoChange.isSelected() == true) {
				if (fileMode.equals("mp3") || fileMode.equals("MP3")
						|| fileMode.equals("WAV") || fileMode.equals("wav")) {

					changToMusicMode();
				} else if (fileMode.equals("mpg") || fileMode.equals("mpeg")
						|| fileMode.equals("MPG") || fileMode.equals("MPEG")
						|| fileMode.equals("AVI") || fileMode.equals("avi")) {

					changToMoveMode(index);
				}
			}
			but_stop.setFocusable(true);
			System.out.println("验证是否有正在播放文件........");
			if (player != null) {
				closePlayerStream();
			}
		}
		return fileDir;
	}

	// 关闭播放对象
	public void closePlayerStream() {
		System.out.println(timeThread);
		if (timeThread != null) {
			((ControlProgrecess) timeThread).stopThread();
		}
		if (player != null) {
			System.out.println("停止上次播放文件!");
			player.stop();
			System.out.println("停止上次播放文件流!");
			player.close();
		}
		if (visual != null) {
			panel_view.remove(visual);
		}
	}

	// 停止播放让窗口还原初始状态
	private void stopMedia() {
		System.out.println("用户上次播放.............");
		if (timeThread != null) {
			((ControlProgrecess) timeThread).stopThread();
		}
		panel_play.remove(but_pause);
		panel_play.add(but_play);
		panel_play.updateUI();
		newTime = 0;
		slider_time.setValue(0);
		firstPlayer = true;
		closePlayerStream();
		this.setTitle("java播放器--淘气一族");
	}

	// 切换到音频模式
	private void changToMusicMode() {
		System.out.println("切换到音频模式.....");
		but_music.setActionCommand("false");
		this.setSize(new Dimension(260, 20));
		this.setResizable(false);
		this.remove(menubar);
		this.remove(splitpane);
		this.pack();
	}

	// 切换到视频模式
	public void changToMoveMode(int index) {

		System.out.println("切换到视频模式.....");
		but_music.setActionCommand("true");
		this.setResizable(true);
		this.setSize(500, 400);
		add(menubar, BorderLayout.NORTH);
		contentPane.add(splitpane, BorderLayout.CENTER);
		popupList.setListData(fileName);
		list.setListData(fileName);
		list.setSelectedIndex(index);
	}

	// 一个类当播放player添加了ControllerListener侦听器后能对歌播放整个过程中进行控制
	private class ControlListener implements ControllerListener {

		public void controllerUpdate(ControllerEvent e) {

			if (e instanceof RealizeCompleteEvent) {
				timeThread = new ControlProgrecess();
				System.out.println("获得文件播放时间: "
						+ player.getDuration().getSeconds());
				fileTime = player.getDuration().getSeconds();
				slider_time.setMaximum((int) fileTime);
				getVisualComponent();

				return;
			}
			if (e instanceof PrefetchCompleteEvent) {
				slider_time.setValue(0);
				mediaMute(but_sounds.getActionCommand());
				player.getGainControl().setLevel(
						(float) jSlider_sound.getValue() / 100);
				System.out.println("获取音量: "
						+ (float) player.getGainControl().getLevel());
				System.out.println("准备完毕!开始播放...................");
				timeThread.start();
				player.start();
				return;
			}
			if (e instanceof ControllerClosedEvent) {
				System.out.println("dokkwewojdeoweowe");
				return;
			}
			if (e instanceof EndOfMediaEvent) {
				firstPlayer = true;
				if(AllScreenOrOriginal = true){
					originalView();
					AllScreenOrOriginal = false;
				}				
				judgePlayerMode();
				newTime = 0;
				slider_time.setValue(0);
				return;
			}

		}

		// 自动判断文件格式并把窗口调成相应模式
		private void judgePlayerMode() {
			System.out.println("正在核对播放模式.......");
			if (menuItem_onlyOneMode.isSelected()) {
				stopMedia();
			} else if (menuItem_forOneMode.isSelected()) {
				pauseToPlayer();
			} else if (menuItem_turnMode.isSelected()) {
				musicChooser(1);
			} else if (menuItem_randomMode.isSelected()) {
				playMedia(randomPodePlayer(list.getLastVisibleIndex()));
			}
		}
	}

	// 随机产生一个播放对象
	public int randomPodePlayer(int listLastIndex) {
		int index = (int) (Math.random() * listLastIndex);
		list.setSelectedIndex(index);

⌨️ 快捷键说明

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