用java写一个简单的音乐播放器

准备工作

准备以下的jar包
用java写一个简单的音乐播放器_第1张图片

代码结构

用java写一个简单的音乐播放器_第2张图片

主要代码

MusicPlayer(启动)

package com.lyrics.musicplayer;

import com.lyrics.musicplayer.ui.Design;

/** 
 * @Author: lyrics
 * @ClassName: MusicPlayer    
 * @Date: 2020/2/19
 * @Copyright: lyrics
 */
public class MusicPlayer {
	
	public static void main(String[] args) {
		try {
			Design window = new Design();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Design 界面设计

package com.lyrics.musicplayer.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;

import java.io.FileInputStream;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.lyrics.musicplayer.service.MyPlayer;
import com.lyrics.musicplayer.util.FileList;

import javazoom.jl.player.Player;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Label;

public class Design {

	protected Shell shell;
	private Text textBrowser;
	String selecteddir=null;
	String programdir=null;
	String music;
	FileInputStream stream = null;
	Player player = null;
	int index = 0;
	boolean statu= true;
	Thread thread ;


	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(358, 335);
		shell.setText("Music Player");
		
		List listDisplay = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		MyPlayer myplayer = new MyPlayer();
		listDisplay.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				index = listDisplay.getSelectionIndex();
			    music=listDisplay.getItem(index);
			    Runnable myRunnable = new Runnable(){
			    	   public void run(){
			    		   System.out.print(music);
			    		   
			    		  myplayer.play(music);
			    		  
			    	     }
			    };
			    thread = new Thread(myRunnable);
			    thread.start();
			    }
			}
	   
		);
		listDisplay.setBounds(10, 138, 322, 149);
		
		
		
		Button btnPrevMusic = new Button(shell, SWT.NONE);
		btnPrevMusic.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				index = index-1;
				music=listDisplay.getItem(index);
			    Runnable myRunnable = new Runnable(){
			    	   public void run(){
			    		  myplayer.play(music);
			    	     }
			    };
			    thread = new Thread(myRunnable);
			    thread.start();
			    }
		
		});
		btnPrevMusic.setBounds(51, 45, 80, 27);
		btnPrevMusic.setText("上一首");
		
		Button btnNextMusic = new Button(shell, SWT.NONE);
		btnNextMusic.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				index = index+1;
				music=listDisplay.getItem(index);
			    Runnable myRunnable = new Runnable(){
			    	   public void run(){
			    		  myplayer.play(music);
			    	     }
			    };
			    thread = new Thread(myRunnable);
			    thread.start();
			    }
		});
		btnNextMusic.setText("下一首");
		btnNextMusic.setBounds(190, 45, 80, 27);
		
		textBrowser = new Text(shell, SWT.BORDER);
		textBrowser.setBounds(10, 93, 229, 23);
		
		Button btnBrowser = new Button(shell, SWT.NONE);
		btnBrowser.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//新建文件夹(目录)对话框
				DirectoryDialog folderdlg=new DirectoryDialog(shell);
				//设置文件对话框的标题
				folderdlg.setText("文件选择");
				//设置初始路径
				folderdlg.setFilterPath("SystemDrive");
				//设置对话框提示文本信息
				folderdlg.setMessage("请选择相应的文件夹");
				//打开文件对话框,返回选中文件夹目录
				selecteddir=folderdlg.open();
				if(selecteddir==null){
				return ;
				}
				else{
					textBrowser.setText(selecteddir);
					programdir=System.getProperty("user.dir");
					try {
						FileList fileList= new FileList();
						Vector<String> list=new Vector<String> ();
						fileList.getAllFileName(selecteddir, list);
						for(int i=0;i<list.size();i++)
						{
							listDisplay.add(list.elementAt(i));
							System.out.println(list.elementAt(i));
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}		
				}
			}
		});
		btnBrowser.setBounds(252, 91, 80, 27);
		btnBrowser.setText("浏览");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(22, 10, 61, 17);
		label.setText("正在播放:");
	}
}

MyPlayer 播放实现

package com.lyrics.musicplayer.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class MyPlayer {
	private FileInputStream stream = null;
	private Player player = null;
	public void play(String music) {
		 File file=new File("F:\\CloudMusic\\"+music);
			if(stream!=null)
			{
				stream=null;
			}
			try {
				stream = new FileInputStream(file);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (player != null) {
				player.close();
			}
			try {
				player = new Player(stream);
			} catch (JavaLayerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				player.play();
			} catch (JavaLayerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	public void next() {
		
	}
	
	public void prev() {
		
	}
}

FileList 获取歌曲

package com.lyrics.musicplayer.util;

import java.io.File;
import java.util.Vector;

public class FileList {
	public void getAllFileName(String path,Vector<String> list) {
	    File file = new File(path);
	    File[] tempList = file.listFiles();

	    for (int i = 0; i < tempList.length; i++) {
	        if (tempList[i].isFile()) {
	            list.add(tempList[i].getName());
	        }
	        if (tempList[i].isDirectory()) {
	            getAllFileName(tempList[i].getAbsolutePath(),list);
	        }
	    }
	    return;
	}
} 

运行结果

用java写一个简单的音乐播放器_第3张图片

写在最后

本代码实现了,歌曲文件夹浏览并添加其中所有歌曲,双击播放,以及上一首、下一首的切换等简单功能。
由于时间关系和个人技术水平限制,代码并不是很清晰,且只实现了一些基本功能,很多功能并没有分开来写,不符合代码规范,所以请谅解。

你可能感兴趣的:(java,SWT/Swing/Awt)