欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java swing仿酷狗音乐播放器

程序员文章站 2023-12-19 09:07:58
今天给大家介绍下用java swing开发一款音乐播放器,高仿酷狗音乐播放器,完整源码地址在最下方,本文只列出部分源码,因为源码很多,全部贴不下,下面还是老规矩。来看看运行...

今天给大家介绍下用java swing开发一款音乐播放器,高仿酷狗音乐播放器,完整源码地址在最下方,本文只列出部分源码,因为源码很多,全部贴不下,下面还是老规矩。来看看运行结果: 

Java swing仿酷狗音乐播放器

Java swing仿酷狗音乐播放器

Java swing仿酷狗音乐播放器

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;
 }
} 

代码就贴这么多。

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

上一篇:

下一篇: