网页音乐播放器

这个播放器是利用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>    返回类型
	* @throws
	 */
	public static ArrayList 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 songArrayList=new ArrayList();
		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 putInfo(String lrc){
		String lines[]=lrc.split("\n");
		ArrayList lrcList=new ArrayList();
		
		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 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 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 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);
	}
}

运行截图



网页音乐播放器_第1张图片

网页音乐播放器_第2张图片


网页音乐播放器_第3张图片

完整项目的下载地址



去下载

你可能感兴趣的:(音乐,搜索)