微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java swing仿酷狗音乐播放器

这篇文章主要为大家详细介绍了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 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 list = new ArrayList(); 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 getDownedSongByDir() { String downedSongDir = getDownloadSongPath(); File downedDir = new File(downedSongDir); if(downedDir.exists() && downedDir.isDirectory()) { String[] fileList = downedDir.list(); for (int i = 0; i list = getDownedSongall(); if(null == list || list.size() 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 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 list = new ArrayList(); 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 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 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; } }

代码就贴这么多。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐