网页音乐播放器

网页音乐播放器这是一款网页版的音乐播放器。这个播放器是利用QQ音乐的api实现了音乐的播放,搜索,歌词同步,音乐的下载。

大家好,又见面了,我是你们的朋友全栈君。

这个播放器是利用qq音乐的api实现了音乐的播放,搜索,歌词同步。

MusicUtil.java主要代码

package com.tc.musicplay.utils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;


import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.show.api.ShowapiRequest;
import com.tc.musicplay.domain.MusicInfo;
import com.tc.musicplay.domain.MusicLrc;


/**
 * 
 */

/**
 * @ClassName: MusicUtil
 * @Description: TODO
 * @author Simple 
 * @date 2017-5-16 上午10:02:19
 *
 */
public class MusicUtil {

	/**
	 * 
	* @Title: searchSongs
	* @Description: 该类是通过歌手,歌名获取音乐列表
	* @param @param keyWord 搜索的关键词
	* @param @param count 每页的个数
	* @param @param page 
	* @param @return    设定文件
	* @return ArrayList<Map<String, String>>    返回类型
	* @throws
	 */
	public static ArrayList<MusicInfo> searchSongs(String keyWord,int count,int page){
		String url="http://s.music.qq.com/fcgi-bin/music_search_new_platform?t=0&n="+count+"&aggr=1&cr=1&loginUin=0&format=json&inCharset=GB2312&outCharset=utf-8¬ice=0&platform=jqminiframe.json&needNewCode=0&p="+page+"&catZhida=0&remoteplace=sizer.newclient.next_song&w="+keyWord;
		ArrayList<MusicInfo> songArrayList=new ArrayList<MusicInfo>();
		String json=HttpUtil.sendGet(url);
		JSONObject rootObject=JSONObject.parseObject(json);
		JSONObject dataObject=rootObject.getJSONObject("data");
		JSONObject songObject=dataObject.getJSONObject("song");
		JSONArray songList=songObject.getJSONArray("list");
		for (int i = 0; i < songList.size(); i++) {
			JSONObject song=songList.getJSONObject(i);
			String f[]=song.getString("f").split("\\|");
			if(f.length>=3){
			MusicInfo musicInfo=new MusicInfo();
			String songId=f[0];
			String imageId=f[f.length-3];
			String songName=song.getString("fsong");
			String singer=song.getString("fsinger");
			String singer2=song.getString("fsinger2");
			musicInfo.setSongUrl(getSongUrl(songId));
			musicInfo.setImageUrl(getImageUrl(imageId));
			musicInfo.setSongName(songName.trim());
			musicInfo.setSinger(singer);
			musicInfo.setSinger2(singer2);
			musicInfo.setSongId(songId);
			songArrayList.add(musicInfo);
			}
			
		}
		return songArrayList;
	}
	/**
	 * 
	* @Title: getSongUrl
	* @Description: 得到歌曲地址
	* @param @param songId
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	public static String getSongUrl(String songId){
		return "http://ws.stream.qqmusic.qq.com/"+songId+".m4a?fromtag=46";
	}
	/**
	 * 
	* @Title: getImageUrl
	* @Description: 得到图片的地址
	* @param @param imageId
	* @param @param width
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	public static String getImageUrl(String imageId){
		String first=imageId.substring(imageId.length()-2,imageId.length()-1);
		String second=imageId.substring(imageId.length()-1);
		return "http://i.gtimg.cn/music/photo/mid_album_300/"+first+"/"+second+"/"+imageId+".jpg";
	}
	/**
	 * 
	* @Title: getSongLrcUrl
	* @Description: 得到歌词的地址
	* @param @param songId
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	public static String getSongLrcUrl(String songId){
		return "http://music.qq.com/miniportal/static/lyric/"+Integer.parseInt(songId)%100+"/"+songId+".xml";
		
	}
	/**
	 * 
	* @Title: downloadSong
	* @Description: 该类是下载歌曲
	* @param @param songId 歌曲的id
	* @param @param destFile 文件类型为mp3
	* @param @throws Exception    设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void downloadSong(String songId,File destFile) throws Exception{
		String songUrl=getSongUrl(songId);
		FileUtils.copyURLToFile(new URL(songUrl), destFile);
		
	}
	public static byte[] downloadSong(String songId){
		
		return null;
	}
	/**
	 * 
	* @Title: downLoadLrc
	* @Description: 该方法是下载歌词
	* @param @param songId
	* @param @param destFile 文件类型为xml
	* @param @throws Exception    设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void downLoadLrc(String songId,File destFile) throws Exception{
		String lrcUrl=getSongLrcUrl(songId);
		FileUtils.copyURLToFile(new URL(lrcUrl), destFile);
	}
	
	private static ArrayList<MusicLrc> putInfo(String lrc){
		String lines[]=lrc.split("\n");
		ArrayList<MusicLrc> lrcList=new ArrayList<MusicLrc>();
		
		Integer offset=0;
		for (int i = 0; i < lines.length; i++) {
			String reg = "\\[(\\d{2}:\\d{2}\\.\\d{2})\\]";  
            // 编译  
            Pattern pattern = Pattern.compile(reg); 
			
			if(lines[i].startsWith("[offset")){
				offset=Integer.parseInt(lines[i].substring(8,lines[i].length()-1));
				System.out.println(offset);
			}else if(pattern.matcher(lines[i]).find()){
				String lrcText=lines[i].substring(10);
				if(lrcText!=null&&!lrcText.equals("")){
					MusicLrc musicLrc=new MusicLrc();
					String time=lines[i].substring(1,9);
					String minute=time.substring(0,2);
					String second=time.substring(3,5);
					String millisecond=time.substring(6);
					int key=(Integer.parseInt(minute)*60+Integer.parseInt(second))*1000+Integer.parseInt(millisecond)*10-offset;
					musicLrc.setTime(key);
					musicLrc.setLrcLine(lrcText);
					lrcList.add(musicLrc);
				}	
			}
		}
		return lrcList;
	}
	public static ArrayList<MusicLrc> getOnlineLrcMap(String songId) throws DocumentException{
		SAXReader reader=new SAXReader();
		String lrcUrl=getSongLrcUrl(songId);
		System.out.println(lrcUrl);
		String result=HttpUtil.sendGet(lrcUrl);
		ByteArrayInputStream byteArrayInputStream=null;
		try {
			byteArrayInputStream = new ByteArrayInputStream(result.getBytes("GB2312"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		Document document=reader.read(byteArrayInputStream);
		Element element=document.getRootElement();
		String lrc=element.getData().toString();
		return putInfo(lrc);
	}
	public static ArrayList<MusicLrc> getLrcMapOnAliyun(String songId){
		ShowapiRequest req = new ShowapiRequest(
				"https://ali-qqmusic.showapi.com/song-word?musicid="+songId,"d5175d863d8c41fba83bc40ca41c6596");
		String json= req.get();
		json=json.replaceAll(":", ":").replaceAll(".", ".").replaceAll("
", "\n").replaceAll(" ", " ");
		System.out.println(json);
		Map map = req.getRes_headMap();
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			Object k = it.next();
			System.out.println(k + "          " + map.get(k));
		}
		JSONObject jsonObject=JSONObject.parseObject(json);
		String lrc=jsonObject.getJSONObject("showapi_res_body").getString("lyric");
		System.out.println(lrc);
		return putInfo(lrc);
	}
	public static ArrayList<MusicLrc> getLrcMap(File file) throws Exception{
		
		SAXReader reader=new SAXReader();
		Document document=reader.read(new FileInputStream(file));
		Element element=document.getRootElement();
		String lrc=element.getData().toString();
		return putInfo(lrc);
	}
}

运行截图

网页音乐播放器网页音乐播放器

网页音乐播放器网页音乐播放器

网页音乐播放器

网页音乐播放器

网页音乐播放器

网页音乐播放器网页音乐播放器

完整项目的下载地址


去下载

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/147933.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月15日 下午12:46
下一篇 2022年6月15日 下午12:46


相关推荐

  • java servlet和jsp区别

    java servlet和jsp区别目前常见的动态网页技术有 CGI ASP PHP JSP 这几种 最早出现的技术是 CGI 通用网关接口 原先的 web 服务器并不支持动态的访问 即实时更新的网页内容 所以出现了 CGI 但是 CGI 运行效率低下 每次访问就会创建一个进程 访问结束就会关闭进程 这样给服务器带来了很大压力 所以 sun 公司就根据 javaapplet 设计出了 javaservlet 技术 javaservlet 是独立于平

    2026年3月16日
    2
  • idea配置tomcat的方法(详细图文步骤)

    idea配置tomcat的方法(详细图文步骤)1 打开 idea 在项目运行列表下拉选择 editConfigur 2 在打开的界面 点击 再选择下面的 TomcatServer 下的 local3 在打开的界面 第一行 Name 中填入 tomcat 的名称然后点击 Configure 在 ApplicationS 界面 点击 在 TomcatServer 配置界面选择要添加的 tomcat

    2026年3月16日
    16
  • 服务器地址和端口号是什么怎么看_常见服务对应的端口号

    服务器地址和端口号是什么怎么看_常见服务对应的端口号常用端口号与对应的服务以及端口关闭端口简介:本文介绍端口的概念,分类,以及如何关闭/开启一个端口21端口:21端口主要用于FTP(FileTransferProtocol,文件传输协议)服务。

    2022年8月4日
    11
  • 【JAVA 课程设计 之 万年历】「建议收藏」

    距离2017年还有30多个小时~转眼间2016只剩一个尾巴了,大学生活也过了快一半了,自己却依旧那么笨手笨脚,不会的知识永远那么多,该看的书永远没机会去看,2017愿一切如昨天抽的签:远方不一定有诗,但有更好的自己~明天你好,请多关照~2017希望我的家人们,小伙伴们,以及所有帮助过我的朋友们都能健健康康,万事如意~Java课设远没有自己想的难,万年历,不用做显示面~也算2016JAVA的最后一

    2022年4月10日
    53
  • HashMap之TreeNode

    HashMap之TreeNodeHashMap 之 TreeNode 简述在分析 HashMap 之前先说一下内部类 TreeNode TreeNode 类是一颗红黑树的各种操作 当然 TreeNode 不只是简单的红黑树操作 还有与 HashMap 业务相关的代码先看一下类的继承关系 Entry 是一个接口 主要有一些让子类去实现的 get set 方法 Node 是一个单向链表最后就是 TreeNode 红黑树了先看一下简单的 Node 单向链表

    2026年3月19日
    1
  • 8000—0004显示设备出现问题_错误0x8007005

    8000—0004显示设备出现问题_错误0x8007005关于COM类工厂80070005和8000401a错误分析及解决办法 问题描述:最近做一个web应用程序需要操作Excel文件,在开发环境下程序测试正常,部署到IIS后程序操作Excel文件,IIS报错,错误出现在创建Excel进程的语句,如下:Application myExcelApp= new ApplicationClass(); IIS提示信息如下:检索 COM 类

    2022年8月20日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号