Java swing仿酷狗音乐播放器
作者:明礼馨德 发布时间:2023-10-24 03:04:34
标签:Java,swing,播放器
今天给大家介绍下用Java swing开发一款音乐播放器, * 酷狗音乐播放器,完整源码地址在最下方,本文只列出部分源码,因为源码很多,全部贴不下,下面还是老规矩。来看看运行结果:
下面我们来看看代码:
首先看一下主窗口的实现代码:
package com.baiting;
import java.awt.Dimension;
import java.awt.Toolkit;
import com.baiting.menu.CloseWindow;
/**
* 窗口
* @author lmq
*
*/
public abstract class MusicWindow extends Music {
protected MusicFrame musicFrame;
private String title;
private int locationX;
private int locationY;
public MusicWindow() {
title = getConfigMap().get("title").toString();
defaultLocation();
}
public MusicWindow(String title,int width,int height) {
this.title = title;
setWidth(width);
setHeight(height);
defaultLocation();
}
public MusicWindow(String title,int width,int height,int locationX,int locationY) {
this.title = title;
setWidth(width);
setHeight(height);
this.locationX = locationX;
this.locationY = locationY;
}
private void defaultLocation() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
locationX = (screenSize.width-getWidth())/2;
locationY = (screenSize.height-getHeight())/2;
}
protected MusicFrame createWindow() {
musicFrame = new MusicFrame();
musicFrame.setTitle(title);
musicFrame.setSize(getWidth(), getHeight());
//musicFrame.setLocation(locationX, locationY);
musicFrame.setLocationRelativeTo(null);
musicFrame.addWindowListener(new CloseWindow());
musicFrame.setMinimumSize(new Dimension(600, 450));
musicFrame.setVisible(true);
return musicFrame;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getLocationX() {
return locationX;
}
public void setLocationX(int locationX) {
this.locationX = locationX;
}
public int getLocationY() {
return locationY;
}
public void setLocationY(int locationY) {
this.locationY = locationY;
}
}
看看在线下载歌曲的代码:
package com.baiting.service;
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.util.ArrayList;
import java.util.List;
import com.baiting.bean.DownFailSong;
import com.baiting.bean.DownedSong;
import com.baiting.http.DownloadSong;
import com.baiting.util.StringUtil;
public class DownloadSongService extends MusicService {
private static DownloadSongService instance;
private static Thread downloadThread;
private DownloadSongService() {
}
public static DownloadSongService getInstance() {
if(null == instance) {
instance = new DownloadSongService();
}
return instance;
}
public void startDownload() {
if(null == downloadThread) {
downloadThread = new Thread(new DownloadSong());
downloadThread.start();
} else {
if(!downloadThread.isAlive()) {
downloadThread.interrupt();
downloadThread = null;
downloadThread = new Thread(new DownloadSong());
downloadThread.start();
}
}
}
public List<DownedSong> getDownedSongAll() {
File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downed.list");
if(!downedListFile.exists()) {
try {
log.info("downed.list文件不存在,正在创建....");
downedListFile.createNewFile();
log.info("downed.list文件创建[成功]....");
} catch (IOException e) {
log.info("downed.list文件创建[失败--异常]....");
e.printStackTrace();
downedListFile = null;
return null;
}
}
try {
BufferedReader reader = new BufferedReader(new FileReader(downedListFile));
String line = reader.readLine();
List<DownedSong> list = new ArrayList<DownedSong>();
int count = 0;
if(!StringUtil.isEmpty(line)) {
while(line != null) {
count++;
String content = line.replace("\n", "").trim();
String[] cols = content.split(SEPARATOR);
if(cols.length>5) {
DownedSong downedSong = new DownedSong();
downedSong.setNo(count);
downedSong.setFileName(cols[0].trim());
downedSong.setSongName(cols[1].trim());
downedSong.setSinger(cols[2].trim());
downedSong.setFileSize(Double.parseDouble(cols[3].trim()));
downedSong.setPath(cols[4].trim());
downedSong.setCreateTime(cols[5].trim());
list.add(downedSong);
downedSong = null;
}
line = reader.readLine();
}
}
reader.close();
reader = null;
if(list.size()>0) {
return list;
}
return null;
} catch (FileNotFoundException e) {
log.info("文件不存在...");
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
downedListFile = null;
}
}
/**
* 扫描目录---未完成
* @return
*/
public List<DownedSong> getDownedSongByDir() {
String downedSongDir = getDownloadSongPath();
File downedDir = new File(downedSongDir);
if(downedDir.exists() && downedDir.isDirectory()) {
String[] fileList = downedDir.list();
for (int i = 0; i < fileList.length; i++) {
}
}
return null;
}
/**
* 判断歌曲是否存在(通过歌曲名和歌手)
* @param songName
* @param singer
* @return
*/
public boolean existSongByInfo(String songName,String singer) {
List<DownedSong> list = getDownedSongAll();
if(null == list || list.size()<1) {
return false;
}
boolean flag = false;
for(DownedSong downedSong : list) {
if(downedSong.getSongName().equals(songName) && downedSong.getSinger().equals(singer)) {
flag = true;
break;
}
}
list = null;
return flag;
}
/**
* 已下载列表中加入新数据
* @param downedSong
* @return
*/
public int addDownedSong(DownedSong downedSong) {
File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downed.list");
if(!downedListFile.exists()) {
try {
log.info("downed.list文件不存在,正在创建....");
downedListFile.createNewFile();
log.info("downed.list文件创建[成功]....");
} catch (IOException e) {
log.info("downed.list文件创建[失败--异常]....");
e.printStackTrace();
downedListFile = null;
return -1;
}
}
if(null != downedSong) {
String contents = downedSong.getFileName()+SEPARATOR+
downedSong.getSongName()+SEPARATOR+downedSong.getSinger()+SEPARATOR+
downedSong.getFileSize()+SEPARATOR+downedSong.getPath()+SEPARATOR+
downedSong.getCreateTime()+"\n";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(downedListFile,true));
writer.write(contents);
writer.flush();
writer.close();
writer = null;
List<DownedSong> lists = getDownedSongAll();
int count = lists.size();
lists = null;
return count;
} catch (IOException e) {
log.info(downedListFile.getName()+"文件信息写入[失败---异常]");
e.printStackTrace();
return -1;
} finally {
downedListFile = null;
downedSong = null;
}
}
return -1;
}
/**
* 获取所有下载失败的歌曲
* @return
*/
public List<DownFailSong> getDownFailSongAll() {
File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
if(!downedListFile.exists()) {
try {
log.info("downFail.list文件不存在,正在创建....");
downedListFile.createNewFile();
log.info("downFail.list文件创建[成功]....");
} catch (IOException e) {
log.info("downFail.list文件创建[失败--异常]....");
e.printStackTrace();
downedListFile = null;
return null;
}
}
try {
BufferedReader reader = new BufferedReader(new FileReader(downedListFile));
String line = reader.readLine();
List<DownFailSong> list = new ArrayList<DownFailSong>();
int count = 0;
if(!StringUtil.isEmpty(line)) {
while(line != null) {
count++;
String content = line.replace("\n", "").trim();
String[] cols = content.split(SEPARATOR);
if(cols.length>3) {
DownFailSong failSong = new DownFailSong();
failSong.setNo(count);
failSong.setSongName(cols[0].trim());
failSong.setSinger(cols[1].trim());
failSong.setFormat(cols[2].trim());
failSong.setFailTime(cols[3].trim());
list.add(failSong);
}
line = reader.readLine();
}
}
reader.close();
reader = null;
if(list.size()>0) {
return list;
}
return null;
} catch (FileNotFoundException e) {
log.info("文件不存在...");
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
downedListFile = null;
}
}
/**
* 已下载列表中加入新数据
* @param downedSong
* @return
*/
public int addDownFailSong(DownFailSong downFailSong) {
File downFailListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
if(!downFailListFile.exists()) {
try {
log.info("downFail.list文件不存在,正在创建....");
downFailListFile.createNewFile();
log.info("downFail.list文件创建[成功]....");
} catch (IOException e) {
log.info("downFail.list文件创建[失败--异常]....");
e.printStackTrace();
downFailSong = null;
return -1;
}
}
if(null != downFailSong) {
String contents = downFailSong.getSongName()+SEPARATOR+downFailSong.getSinger()+SEPARATOR+
downFailSong.getFormat()+SEPARATOR+downFailSong.getFailTime()+"\n";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(downFailListFile,true));
writer.write(contents);
writer.flush();
writer.close();
List<DownFailSong> lists = getDownFailSongAll();
return lists.size();
} catch (IOException e) {
log.info(downFailListFile.getName()+"文件信息写入[失败---异常]");
e.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
downFailSong = null;
contents = null;
}
}
return -1;
}
/**
* 删除下载失败的歌曲列表
* @param no
* @return
*/
public boolean delDownFailSong(int no) {
List<DownFailSong> lists = getDownFailSongAll();
if(null != lists && lists.size()>0 && lists.size()>=no && no>0) {
DownFailSong downFailSong = lists.get(no-1);
log.info("删除下载失败的歌曲《"+downFailSong.getSongName()+"》");
lists.remove(downFailSong);
StringBuffer strBuff = new StringBuffer();
if(null != lists && lists.size()>0) {
for(DownFailSong fs : lists) {
String contents = fs.getSongName()+SEPARATOR+fs.getSinger()+SEPARATOR+
fs.getFormat()+SEPARATOR+fs.getFailTime()+"\n";
strBuff.append(contents);
}
} else {
strBuff.append("");
}
File downFailListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
if(!downFailListFile.exists()) {
try {
log.info("downFail.list文件不存在,正在创建....");
downFailListFile.createNewFile();
log.info("downFail.list文件创建[成功]....");
} catch (IOException e) {
log.info("downFail.list文件创建[失败--异常]....");
e.printStackTrace();
return false;
} finally {
lists = null;
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(downFailListFile,false));
writer.write(strBuff.toString());
writer.flush();
writer.close();
log.info("删除下载失败的歌曲《"+downFailSong.getSongName()+"》--成功---");
return true;
} catch (IOException e) {
log.info(downFailListFile.getName()+"文件信息写入[失败---异常]");
e.printStackTrace();
return false;
} finally {
lists = null;
downFailListFile = null;
downFailSong = null;
}
}
return false;
}
}
代码就贴这么多。
来源:http://blog.csdn.net/llqqxf/article/details/51898965
0
投稿
猜你喜欢
- java 8的新特性之一就是lambda表达式,parallelStream()都说性能会比较高,现一探究竟。话不多说,上代码: @Test
- 类似普通对象,通过new创建字符串对象。String str = new String("Hello"); 内存图如下图
- itextpdf解决PDF合并的问题本文章是我在项目开发过程中解决了一个关于PDF显示的需求而记录的。需求是这样的,需要将两个PDF进行合并
- 背景使用flowable自带的flowable-ui制作流程图使用springboot开发流程使用的接口完成流程的业务功能一、flowabl
- 一、前言使用动态配置的原因: properties 和 yaml 是写到项目中的,好多时候有些配置需要修改,每次修改就要重新启动项目,不仅增
- Sentinel是阿里巴巴开源的限流器熔断器,并且带有可视化操作界面。在日常开发中,限流功能时常被使用,用于对某些接口进行限流熔断,譬如限制
- Java使用OpenCV3.2实现视频读取与播放,供大家参考,具体内容如下OpenCV从3.x版本开始其JAVA语言的SDK支持视频文件读写
- Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户
- Google 发布的Material Design支持库,对我们的APP设计有很大的影响,如果重新设计APP,支持库应该直接用V4提升到V7
- 1、lock是可中断锁,而synchronized 不是可中断锁线程A和B都要获取对象O的锁定,假设A获取了对象O锁,B将等待A释放对O的锁
- 优点1.观察者和被观察者是抽象耦合的。2.建立一套触发机制。缺点1.如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知
- 关于base64编码Encode和Decode编码的几种方式Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个B
- 情况简介spring项目,controller异步调用service的方法,产生大量并发。具体业务:前台同时传入大量待翻译的单词,后台业务接
- 看代码吧~package com.mtpc.admin.controller.exportSql;import ch.qos.logback
- 开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8一、发布REST服务1、IDEA新建一个名称为r
- 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有
- 前言我们知道,Java项目编译后会生成许许多多的class文件,class文件保存着类的描述信息。虚拟机把描述类的数据从Class文件加载到
- Interceptor讲到Interceptor,相信熟悉struts2的童鞋肯定不会陌生了,struts2可以自定义 * 进行自己想要的一
- .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管window
- 背景传统 SpringMVC 项目中,我们可以定义容器初始化 Servlet ,然后在 web.xml 配置该 Servlet ,指定 lo