Example #1
0
        /// <summary>
        /// 下载红心歌曲
        /// </summary>
        public void Download()
        {
            if (Directory.Exists(Path) == false)//如果不存在就创建文件夹
            {
                Directory.CreateDirectory(Path);
            }
            client.NewList();
            client.SwitchChannel(Channel.RedHeart);
            int times = 0;
            int num   = 0;

            while (dic.Count <= Total)
            {
                foreach (Song song in client.Playlist)
                {
                    if (!dic.Keys.Contains(song.SongID))
                    {
                        dic.Add(song.SongID, song);
                        num++;
                        currentSong = song;
                        string fileName = Path + song.Title + ".mp3";
                        try
                        {
                            image = Image.FromStream(WebRequest.Create(song.PictureURL).GetResponse().GetResponseStream());
                        }
                        catch (Exception)
                        {
                            image = Properties.Resources.cover;
                        }
                        NewSong.Invoke(num, image, song.Title, song.Artist);
                        if (File.Exists(fileName))
                        {
                            Exist++;
                        }
                        else
                        {
                            if (Downfile(song.MediaURL, fileName))
                            {
                                Success++;
                                ID3Info info = new ID3Info(fileName, true);
                                info.Load();
                                info.ID3v2Info.SetMinorVersion(3);
                                AttachedPictureFrame pic = null;
                                try
                                {
                                    if (!string.IsNullOrEmpty(CoverPath))//保存封面
                                    {
                                        image.Save(CoverPath + song.Title + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                    }

                                    ms = new System.IO.MemoryStream();
                                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    // 创建新封面
                                    pic = new AttachedPictureFrame(
                                        FrameFlags.Compression, "cover.jpg", TextEncodings.UTF_16, "",
                                        AttachedPictureFrame.PictureTypes.Cover_Front, ms);
                                    info.ID3v2Info.AttachedPictureFrames.Add(pic);
                                    // 添加新封面到MP3中
                                }
                                catch
                                {
                                }

                                if (!string.IsNullOrEmpty(lyricPath))//下载歌词
                                {
                                    downloadLrc.SaveLyric(song.Artist, song.Title, lyricPath + song.Title + ".lrc");
                                }

                                // 设置其它属性(ID3V1)
                                info.ID3v1Info.Title  = song.Title;
                                info.ID3v1Info.Artist = song.Artist;
                                info.ID3v1Info.Album  = song.AlbumTitle;

                                //jasine fixed 2015.9.23
                                info.ID3v1Info.Year = song.PublicationTime;
                                //info.ID3v1Info.Year = "";

                                info.ID3v1Info.Comment = "*****@*****.**"; //注释---------添加不上去
                                info.ID3v1Info.Genre   = 12;                     //类型代码Other:12 对应于ID3V2中自定义类型RedHeart

                                // 设置其它属性(ID3V2)
                                info.ID3v2Info.SetTextFrame("TIT2", song.Title);
                                info.ID3v2Info.SetTextFrame("TPE1", song.Artist);
                                info.ID3v2Info.SetTextFrame("TALB", song.AlbumTitle);

                                //jasine fixed 2015.9.23
                                info.ID3v2Info.SetTextFrame("TYER", song.PublicationTime);
                                //info.ID3v2Info.SetTextFrame("TYER", "");

                                //info.ID3v2Info.SetTextFrame("COMM", "*****@*****.**");//注释---------添加不上去
                                info.ID3v2Info.SetTextFrame("TCON", "RedHeart");//类型

                                //jasine fixed 2015.9.23
                                //info.ID3v2Info.SetTextFrame("TPUB", song.Publisher);//发布者,发布单位
                                info.ID3v2Info.SetTextFrame("TPUB", "");//发布者,发布单位

                                // 保存到MP3中
                                if (ID3Version == 1)
                                {
                                    info.ID3v1Info.Save();
                                }
                                else
                                {
                                    //info.Save(3);
                                    info.ID3v2Info.PopularimeterFrames.Add(new ID3.ID3v2Frames.TextFrames.PopularimeterFrame(
                                                                               FrameFlags.TagAlterPreservation, "fm.douban.com", song.Rating, 0));//豆瓣评分
                                    info.ID3v2Info.Save(3);
                                }
                            }
                            else
                            {
                                //下载失败
                                Fail++;
                            }
                        }
                        if ((Success + Fail + Exist) >= Total)
                        {
                            Msg.Invoke(Success, Fail, Exist, "下载完成");
                            Stop();
                        }
                        else
                        {
                            Msg.Invoke(Success, Fail, Exist, "");
                        }
                    }
                }
                client.Skip();
                times++;
                if (times > 10000)
                {
                    Fail = Total - (Success + Exist);
                    Msg.Invoke(Success, Fail, Exist, "尝试次数过多,未成功下载歌曲请过几小时后再在原文件夹下载");
                    Stop();
                }
            }
            Msg.Invoke(Success, Fail, Exist, "下载完成");
        }