|
笔者是音乐爱好者,喜欢抖音上的各种音乐。 于是制作了这个小软件,它功能很简单: 利用酷狗接口搜索关键字音乐 在线播放音乐 显示歌词 可以下载音乐
这个小程序用到了Json解析,还有http下载网络资源等技术。请参考下面的源代码:
using Cosmokey.Utility;using netMarketing;using Newtonsoft.Json;using Newtonsoft.Json.Linq;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using www.skcircle.com;using System.Speech.Synthesis;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Net;namespace music{ public partial class Form1 : Form { [Serializable public class musicStruc { public string SongName; public string FileHash; public string AlbumID; public string imgurl; public string lyrics; public string play_url; } [Serializable public class configStruc { public string searchtxt; public string savePath; public Image img; public string songname; public string lyrics; public string musicUrl; } private List<musicStruc> musicList = new List<musicStruc>(); configStruc config=new configStruc(); httpClass http = new httpClass(); public Form1() { InitializeComponent(); } private int readMusicList(string keyword,int pageNums=1,int pagesize=1) { var url = string.Format( "http://songsearch.kugou.com/song_search_v2?keyword={0}&page={1}&pagesize={2}&platform=WebFilter", httpClass.UrlDecode("抖音"), pageNums, pagesize); string jsonData = http.sendWebRequet(url); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonData); musicList.Clear(); for (int i = 1; i < pagesize; i++) { var t = jo["data"["lists"[i; musicStruc data = new musicStruc(); data.SongName = t["SongName".ToString(); data.FileHash = t["FileHash".ToString(); data.AlbumID = t["AlbumID".ToString(); musicList.Add(data); } return musicList.Count; } private void playerMusicByid(string Hash) { string AlbumID = "", songName = ""; var list = musicList.Where(s => s.FileHash == Hash); if (list.Count() > 0) { AlbumID = list.FirstOrDefault().AlbumID; songName = list.FirstOrDefault().SongName; } else return; var url2 = string.Format( "http://www.kugou.com/yy/index.php?r=play/getdata&hash={0}&album_id={1}&=", Hash, AlbumID); string jsondata2 = http.sendWebRequet(url2); JObject jo = (JObject)JsonConvert.DeserializeObject(jsondata2); var t = jo["data"; var imgurl = t["img".ToString(); var lyrics = t["lyrics".ToString(); var play_url = t["play_url".ToString(); config.songname = songName; config.lyrics = lyrics; config.img = http.DowloadCheckImg(imgurl); config.musicUrl = play_url; var list1=musicList.Where(s => s.FileHash == Hash); list1.FirstOrDefault().imgurl = imgurl; list1.FirstOrDefault().lyrics = lyrics; list1.FirstOrDefault().play_url = play_url; setUIfromConfig(config); } private void setUIfromConfig(configStruc data) { label1.Text =data.songname; picMusicImg.Image =data.img; rtblyrics.Text =data.lyrics; axWindowsMediaPlayer1.URL =data.musicUrl; tbSearchtxt.Text = data.searchtxt; tbMusicDownPath.Text = data.savePath; } private List<string> getMusicItems() { var list = new List<string>(); foreach(var m in musicList) { list.Add(m.SongName + "," + m.FileHash); } return list; } private void btnGetMusicList_Click(object sender, EventArgs e) { var txt = tbSearchtxt.Text; if (txt.Length >= 2) { config.searchtxt = txt; readMusicList(txt, 1, 20); clbMusicList.Items.Clear(); clbMusicList.Items.AddRange(getMusicItems().ToArray()); } } private void clbMusicList_MouseClick(object sender, MouseEventArgs e) { var obj = (CheckedListBox)sender; var txt = obj.SelectedItem.ToString(); if (txt.Length > 0) { playerMusicByid(txt.Split(',')[1); } } private void Form1_Load(object sender, EventArgs e) { loadLastUiData(); } private void loadLastUiData() { var list1 = (List<musicStruc>)Serialize.FileDeSerialize(AppDomain.CurrentDomain.BaseDirectory + "data"); if (list1 != null) { musicList = list1; clbMusicList.Items.Clear(); clbMusicList.Items.AddRange(getMusicItems().ToArray()); } object list2 =Serialize.FileDeSerialize(AppDomain.CurrentDomain.BaseDirectory + "config"); if (list2 != null) { config = (configStruc)list2; setUIfromConfig(config); } } private void saveLastUidata() { if (musicList != null) { Serialize.FileSerialize(AppDomain.CurrentDomain.BaseDirectory + "data", musicList); config.savePath = tbMusicDownPath.Text; config.searchtxt = tbSearchtxt.Text; Serialize.FileSerialize(AppDomain.CurrentDomain.BaseDirectory + "config", config); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { saveLastUidata(); } private void btnDownSelectMusic_Click(object sender, EventArgs e) { var txt=tbMusicDownPath.Text; var selmusic = clbMusicList.SelectedItem.ToString(); var f1 = false; foreach(var m in clbMusicList.CheckedItems) { f1 = true; break; } if(!Directory.Exists(txt)) { Directory.CreateDirectory(txt); } if(Directory.Exists(txt) && selmusic.Length>0 && f1) { config.savePath = txt; var hashid = selmusic.Split(',')[1; foreach(var m in musicList) { if(m.FileHash==hashid) { var f2=http.Download(m.play_url,config.savePath+"\\"+config.songname+".mp3"); if (f2) MessageBox.Show(m.SongName + " 下载完成!"); else MessageBox.Show(m.SongName + " 下载失败!"); break; } } } } } }
music.rar
(388.4 KB, 下载次数: 101)
|
|