Java swing仿酷狗音乐播放器

 更新时间:2017年06月05日 11:04:50   作者:明礼馨德  
这篇文章主要为大家详细介绍了Java swing实现音乐播放器,Java开发图形界面程序音乐播放器仿酷狗音乐播放器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天给大家介绍下用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;
 }
} 

代码就贴这么多。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring核心容器之BeanDefinition解析

    Spring核心容器之BeanDefinition解析

    这篇文章主要介绍了Spring核心容器之BeanDefinition解析,Spring 将管理的对象称之为 Bean,容器会先实例化 Bean,然后自动注入,实例化的过程就需要依赖 BeanDefinition,需要的朋友可以参考下
    2023-11-11
  • Spring Cloud Config 使用本地配置文件方式

    Spring Cloud Config 使用本地配置文件方式

    这篇文章主要介绍了Spring Cloud Config 使用本地配置文件方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 浅谈Java自定义注解相关知识

    浅谈Java自定义注解相关知识

    今天带大家来学习Java注解的相关知识,文中对自定义注解作了非常详细的介绍,对正在学习Java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • JavaWeb实体类转为json对象的实现方法

    JavaWeb实体类转为json对象的实现方法

    这篇文章主要介绍了JavaWeb实体类转为json对象的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring如何按业务模块输出日志到不同的文件详解

    Spring如何按业务模块输出日志到不同的文件详解

    最近做项目时有一个记录操作日志的需求,比如某个用户进行了查询、删除、修改等操作,下面这篇文章主要给大家介绍了关于Spring如何按业务模块输出日志到不同文件的相关资料,需要的朋友可以参考下
    2022-05-05
  • java编程实现求质数与因式分解代码分享

    java编程实现求质数与因式分解代码分享

    这篇文章主要介绍了Java编程实现求质数与因式分解代码分享,对二者的概念作了简单介绍(多此一举,哈哈),都是小学数学老师的任务,然后分享了求解质数和因式分解的Java代码,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • SpringBoot通过整合Dubbo解决@Reference注解问题

    SpringBoot通过整合Dubbo解决@Reference注解问题

    这篇文章主要介绍了SpringBoot通过整合Dubbo解决@Reference注解问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringMVC拦截器快速掌握上篇

    SpringMVC拦截器快速掌握上篇

    拦截器(Interceptor)是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行。本文将详细讲讲SpringMVC中拦截器的概念及入门案例,感兴趣的可以尝试一下
    2022-08-08
  • SpringBoot HikariCP配置项及源码解析

    SpringBoot HikariCP配置项及源码解析

    这篇文章主要为大家介绍了SpringBoot HikariCP配置项及源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 详解如何在Java中加密和解密zip文件

    详解如何在Java中加密和解密zip文件

    在本文中,我们来学习如何用Zip4j库创建受密码保护的压缩文件并将其解压,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-09-09

最新评论