Exemple #1
0
 public IActionResult Get(string type, string id)
 {
     try
     {
         string json    = MusicApi.Search(id, 1, 0, 1);
         var    jsonObj = JsonConvert.DeserializeObject <dynamic>(json);
         var    result  = jsonObj.result;
         if (result != null)
         {
             var songs = result.songs;
             if (songs != null)
             {
                 string musicId       = songs[0].id;
                 string detailjson    = MusicApi.Detail(musicId);
                 var    detailjsonObj = JsonConvert.DeserializeObject <dynamic>(detailjson);
                 string lyricjson     = MusicApi.Lyric(musicId);
                 var    lyricjsonObj  = JsonConvert.DeserializeObject <dynamic>(lyricjson);
                 string lyric         = string.Empty;
                 if (lyricjsonObj.nolyric == null && lyricjsonObj.uncollected == null)
                 {
                     lyric = lyricjsonObj.lrc.lyric;
                 }
                 var url    = $"http://music.163.com/song/media/outer/url?id={musicId}.mp3";
                 var name   = songs[0].name;
                 var artist = songs[0].artists[0].name;
                 var cover  = detailjsonObj.songs[0].album.picUrl;
                 return(Json(new { code = 0, url = url, name = name, artist = artist, cover = cover, lyric = lyric, msg = "" }));
             }
         }
         return(Json(new { code = 1, msg = "获取音乐链接失败" }));
     }
     catch (Exception ex)
     {
         return(Json(new { code = 1, msg = ex.ToString() }));
     }
 }
Exemple #2
0
        public string GetCover(string songId)
        {
            try
            {
                string detailjson = MusicApi.Detail(songId);
                if (string.IsNullOrEmpty(detailjson))
                {
                    haveLyric = false; return(null);
                }
                var detailjsonObj = JsonConvert.DeserializeObject <dynamic>(detailjson);
                if (detailjsonObj == null)
                {
                    haveLyric = false; return(null);
                }
                var    artists  = detailjsonObj.songs[0].artists;
                var    album    = detailjsonObj.songs[0].album.name;
                string duration = detailjsonObj.songs[0].duration;
                if (duration != null)
                {
                    try
                    {
                        TimeSpan ts = TimeSpan.FromMilliseconds(double.Parse(duration));
                        MaxLength            = ts.TotalSeconds;
                        txtTotalSeconds.Text = TimeSpanToDateTime(ts);
                    }
                    catch { }
                }

                if (album != null)
                {
                    playerInfo.Album = album;
                }

                //获取歌词
                string lyricjson    = MusicApi.Lyric(songId);
                var    lyricjsonObj = JsonConvert.DeserializeObject <dynamic>(lyricjson);
                string lyric        = string.Empty;
                if (lyricjsonObj != null && lyricjsonObj.nolyric == null && lyricjsonObj.uncollected == null)
                {
                    lyric = lrcTemp = lyricjsonObj.lrc.lyric;
                }
                else
                {
                    lyric = lrcTemp = string.Empty;
                }

                List <string> listArtists = new List <string>();
                if (artists != null)
                {
                    foreach (var item in artists)
                    {
                        listArtists.Add((string)item.name);
                    }
                    string strArtists = string.Join("/", listArtists);
                    mySongList[listViewSongList.SelectedIndex].artist = strArtists;
                }
                else
                {
                    mySongList[listViewSongList.SelectedIndex].artist = "未知";
                }


                //mySongList[listViewSongList.SelectedIndex].lyric = lyric;

                if (!string.IsNullOrEmpty(lyric))
                {
                    //byte[] array = Lrc.ByteArrayCut(Encoding.Default.GetBytes(lyric), 0x3f);
                    //将默认编码转到Unicode
                    byte[] arraydefalut = Encoding.UTF8.GetBytes(lyric);
                    byte[] arrayunicode = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, arraydefalut);

                    MemoryStream stream = new MemoryStream(arrayunicode);
                    lrc       = Lrc.InitLrc(stream);
                    haveLyric = true;
                    //有歌词
                    DoShowLrc("");
                }
                else
                {
                    haveLyric = false;
                    DoShowLrc("没有歌词");
                }

                return(detailjsonObj.songs[0].album.picUrl);
            }
            catch (Exception)
            {
                haveLyric = false;
                DoShowLrc("歌词获取失败");

                return(string.Empty);
            }
        }
Exemple #3
0
 public HttpResponseMessage Detail(string ids = null)
 {
     return(Json(MusicApi.Detail(ids)));
 }