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

📄 mp3player.java

📁 Java音乐播放器源码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.awt.Component;
import java.net.*;
public class Mp3Player extends JFrame implements Runnable,ActionListener
{
	private JMenuBar menubar;
	
	private JMenu   menufile,About;
	private JMenuItem opendir,exit,info;

	private JPopupMenu popupmenu;
	private JMenuItem  delfile,singleloop;
	
	
	private JList songsList;
	private	JScrollPane scroll;
	JPanel 	panel;
	private File file;
	private String[] filepath;
	private	String[] sFilesValid;
	private int index=0;
	private int totalnum;
	private boolean newdir=false;
	private boolean firststart=true;
	private boolean firstOpenDir=true;
	private boolean flagsingleloop=false;
	private Container c; 
	Thread thread;
	Player videoPlayer;
	Component timeComponent=null;
	public Mp3Player()
	{
		super("Mp3Player!");

		popupmenu=new JPopupMenu();
		singleloop=new JMenuItem("单曲循环");
		delfile=new JMenuItem("删除");
	
		singleloop.addActionListener(this);
		delfile.addActionListener(this);
		popupmenu.add(singleloop);
		popupmenu.add(delfile);

		menubar=new JMenuBar();
		menufile=new JMenu("文件");
		opendir=new JMenuItem("打开文件夹");
		exit=new JMenuItem("退出");
		About=new JMenu("关于");
		info=new JMenuItem("版本信息");
		filepath=new String[200];
		c=getContentPane();
		menufile.add(opendir);
		opendir.addActionListener(this);
		menufile.add(exit);
		exit.addActionListener(this);
		About.add(info);
		info.addActionListener(this);
		menubar.add(menufile);
		menubar.add(About);
		c.setLayout(new BorderLayout());
		c.add(menubar,"North");
		songsList=new JList();
		songsList.addMouseListener(new MouseAdapter() 
		{ 
			public void mouseClicked(MouseEvent e) 
			{ 
				if (e.getClickCount() == 2) 
				{ 
				    index=songsList.locationToIndex(e.getPoint()); 
				    createPlayer();
				}
				if (e.getClickCount() == 1) 
				{ 
				    index=songsList.locationToIndex(e.getPoint());
				    songsList.setSelectedIndex(index);
				    songsList.setSelectionForeground(Color.BLUE);
				}
			}
			public void mousePressed(MouseEvent e) 
			{
				this_mousePressed(e);	
			}	
			
		});
		
		
		songsList.addKeyListener(new KeyAdapter() 
		{
			public void keyReleased(KeyEvent e)
			{
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
				{
				    	createPlayer();
				}
				if(e.getKeyCode()==KeyEvent.VK_DOWN)
				{
					if(index==totalnum-1)
					{
						index=0;
						songsList.setSelectedIndex(index);
						songsList.setSelectionForeground(Color.BLUE); 
					}
					else 
					{
						index+=1;
						songsList.setSelectedIndex(index);
						songsList.setSelectionForeground(Color.BLUE); 
					}	
				}
				if(e.getKeyCode()==KeyEvent.VK_UP)
				{
					if(index==0)
					{
						index=totalnum-1;
						songsList.setSelectedIndex(index);
						songsList.setSelectionForeground(Color.BLUE); 
					}
					else 
					{
						index-=1;
						songsList.setSelectedIndex(index);
						songsList.setSelectionForeground(Color.BLUE); 
					}	
				}
					
			}
		});
		scroll=new JScrollPane(songsList);
		panel=new JPanel();
		c.add(scroll,"Center");
		setSize(400,400);
		setVisible(true);
	}
	public  void actionPerformed(ActionEvent e) 
	{ 
		if(e.getSource()==opendir) 
		{ 
			openDir(); 
		}
		if(e.getSource()==exit) 
		{ 
			Exit(); 
		}
		if(e.getSource()==info)
		{
			JOptionPane.showMessageDialog(this,"Written by yanhualiang,version 1.0.","Author:yanhualiang!",
					JOptionPane.INFORMATION_MESSAGE); 	
		}
		if(e.getSource()==delfile)
		{
				int chooseValue=JOptionPane.showConfirmDialog(this, 
							"确定要将该文件从播放列表中删除吗?","删除文件", 
							JOptionPane.YES_NO_OPTION);
				if(chooseValue==JOptionPane.OK_OPTION)
				{
					if(index==totalnum-1)
						index=0;
					else 
						for(int i=index+1;i<totalnum;i++)
						{
							filepath[i-1]=filepath[i];
							sFilesValid[i-1]=sFilesValid[i];
						}
					sFilesValid[totalnum-1]=null;
					filepath[totalnum-1]=null;
					totalnum--;
					songsList.setListData(sFilesValid);
					createPlayer();			
				}
		}
		if(e.getSource()==singleloop)
		{
			if(flagsingleloop)
			{
				flagsingleloop=false;
			}
			else 
			{
				flagsingleloop=true;
			}
				
		}
	}
	public void this_mousePressed(MouseEvent e) 
	{
		int mods=e.getModifiers();
		//鼠标右键
		if((mods&InputEvent.BUTTON3_MASK)!=0)
		//弹出菜单
		{
			if(flagsingleloop)
				singleloop.setText("取消单曲循环");
			else 
				singleloop.setText("单曲循环");	
			popupmenu.show(songsList,e.getX(),e.getY());
		}
	}
	protected void processWindowEvent(WindowEvent e)
  	{
    		super.processWindowEvent(e);
    		if (e.getID() == WindowEvent.WINDOW_CLOSING)
    		{
      			System.exit(0);
    		}
  	}
	public void openDir() 
	{
		JFileChooser fileChooser=new JFileChooser(new File("F:\\songs")); 
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 		
		int result=fileChooser.showOpenDialog(Mp3Player.this); 
		if(result==JFileChooser.CANCEL_OPTION) 
					return; 
		file=fileChooser.getSelectedFile(); 
		if(file==null||file.getName().equals("")) 
		JOptionPane.showMessageDialog(this,"错误的路径", 
			"出错了",JOptionPane.ERROR_MESSAGE); 
		String[] sFiles=file.list();
	       	sFilesValid=new String[sFiles.length];	
		totalnum=0;
		for(int i=0,j=0;i<sFiles.length;i++) 
		{ 
			if(sFiles[i].endsWith("mp3"))
			{
				filepath[j]=file.getAbsolutePath()+"\\"+sFiles[i];
				sFilesValid[j++]=sFiles[i];
				totalnum++;	
			}
		}	
		songsList.setListData(sFilesValid);
		newdir=true;
		if(firstOpenDir==false&&firststart==true)
				firststart=false;
		if(firstOpenDir==true)
			firstOpenDir=false;
	}
	public void Exit()
	{
		System.exit(0);
	}
	public void createPlayer() 
	{
		
		thread=new Thread(this);
		thread.start();	
	}
	public void run()  
	{
		if(videoPlayer!=null)
		{
			videoPlayer.stop();
			videoPlayer.close();	
		}
		BorderLayout borderLayout1 = new BorderLayout();
		try
		{	
			File videoFile=new File(filepath[index]);
			songsList.setSelectedIndex(index);
			songsList.setSelectionForeground(Color.BLUE); 
			videoPlayer=Manager.createRealizedPlayer(videoFile.toURL());
		//videoPlayer=Manager.createPlayer(new MediaLocator(args[0]));  
		//player1.addControllerListener(this);
		videoPlayer.addControllerListener(new ControllerHand());
              	videoPlayer.prefetch();
              	panel.setLayout(borderLayout1);
		if(timeComponent!=null)
		{
			panel.remove(timeComponent);
			timeComponent=null;
		}
		timeComponent=videoPlayer.getControlPanelComponent();
		panel.setSize(new Dimension(400, 50));
		panel.add(timeComponent,"North");
       		//fr.getContentPane().add(voiceComponent);
       		getContentPane().add(panel,"South");
		setVisible(true);
       		videoPlayer.start();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	
	}
	private class ControllerHand implements ControllerListener 
	{ 
	public void controllerUpdate(ControllerEvent e) 
	{ 
		if (e instanceof EndOfMediaEvent) 
		{
       		//	index=(index+1)%filepath.length;
		
			
			if(flagsingleloop)
			{
				;
			}
			else
			{			
				if((index==totalnum-1)||(newdir==true&&firststart==false))
				{
					index=0;
					newdir=false;	
				}
				else 
				{
					index+=1;
					newdir=false;
				}
				if(firststart==true)
					firststart=false;
			}
       			createPlayer();       
		}
	}
}	
	public static void main(String[] args)throws IOException,NoPlayerException,CannotRealizeException,MalformedURLException
	{
		Mp3Player mp3player=new Mp3Player();	
	}
}

⌨️ 快捷键说明

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