/// <summary> /// 获得歌曲文件信息 /// </summary> /// <param name="filePath">源歌曲文件路径</param> /// <param name="flag">标志位,根据设置而定</param> public MusicInfo(string filePath, int flag) { try { // 尝试获取ID3标签 ID3Info id3 = new ID3Info(filePath, true); Title = id3.ID3v1Info.Title != null ? id3.ID3v1Info.Title : id3.ID3v2Info.GetTextFrame("TIT2"); Singer = id3.ID3v1Info.Artist != null ? id3.ID3v1Info.Artist : id3.ID3v2Info.GetTextFrame("TPE1"); if (Title.Equals("")) { // 尝试手工分割文件名 string fileName = Path.GetFileNameWithoutExtension(filePath); Title = fileName; Singer = fileName; } } catch (Exception ex) { // 尝试手工分割文件名 string fileName = Path.GetFileNameWithoutExtension(filePath); Title = fileName; Singer = fileName; } MusicPath = filePath; }
private void _currentMenu_Click(object sender, EventArgs e) { #region 界面归零 module.MainListBox.Items.Clear(); module.MusicPathList.Clear(); module.MainProgressBar.Maximum = 0; module.MainProgressBar.Value = 0; #endregion FolderBrowserDialog _fd = new FolderBrowserDialog(); _fd.Description = "请选择歌曲与歌词所在的文件夹"; _fd.ShowDialog(); string[] files = null; if (_fd.SelectedPath != "") { files = Directory.GetFiles(_fd.SelectedPath, "*.mp3",SearchOption.AllDirectories); foreach (var item in files) { module.MainListBox.Items.Add(new ListViewItem(new string[] { item, "" })); } // 写入 module.MainProgressBar.Maximum = files.Length; int _count = 0; module.MainStatusStrip.Text = "开始写入LRC歌词文件..."; foreach (var item in files) { var _str = Path.GetDirectoryName(item) + @"\" + Path.GetFileNameWithoutExtension(item) + ".lrc"; if(File.Exists(_str)) { var _fr = new FileStream(_str, FileMode.Open); var _sr = new StreamReader(_fr); var _text = _sr.ReadToEnd(); try { // 写入ID3标签 ID3Info info = new ID3Info(item, true); info.ID3v2Info.SetTextFrame("TEXT", _text); info.Save(); module.MainListBox.Items[_count].SubItems[1].Text = "成功"; } catch { module.MainListBox.Items[_count].SubItems[1].Text = "失败"; } _sr.Close(); _fr.Close(); } else { module.MainListBox.Items[_count].SubItems[1].Text = "不存在LRC"; } _count++; module.MainProgressBar.Value++; } module.MainStatusStrip.Text = "歌词写入完成!"; } }
/// <summary> /// 构造函数 /// </summary> /// <param name="FilePath">文件路径</param> public ID3V2Tag(string FilePath) { ID3Info info = new ID3Info(FilePath, true); foreach (AttachedPictureFrame AP in info.ID3v2Info.AttachedPictureFrames.Items) { TrackImages.Add(Image.FromStream(AP.Data)); } ////曲名 TrackTitleName = info.ID3v2Info.GetTextFrame("TIT2"); ////艺术家 ArtistName = info.ID3v2Info.GetTextFrame("TPE1"); ////专辑 AlbumName = info.ID3v2Info.GetTextFrame("TALB"); ////音轨 TrackNo = info.ID3v2Info.GetTextFrame("TRCK").Trim(); ////碟号 DiscNo = info.ID3v2Info.GetTextFrame("TPOS").Trim(); ////年份 SalesYear = info.ID3v2Info.GetTextFrame("TYER").Trim(); if (TrackNo == string.Empty) TrackNo = "1"; if (DiscNo == string.Empty) DiscNo = "1"; string SongName = Path.GetFileName(FilePath);//获得歌曲名称 FileInfo fInfo = new FileInfo(FilePath); Shell sh = new ShellClass(); Folder dir = sh.NameSpace(FilePath.Substring(0, FilePath.LastIndexOf(@"\"))); FolderItem item = dir.ParseName(SongName); try { MainFormat format = new MainFormat(); this.TrackLength = dir.GetDetailsOf(item, 27); this.BitRate = dir.GetDetailsOf(item, 28); } catch { } }
/// <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, "下载完成"); }
private void button1_Click(object sender, EventArgs e) { /*if(listBox1.SelectedItem.ToString() != null) * { * of1.FileName = listBox1.SelectedItem.ToString(); * if (of1.FileName != null) * { * ID3.ID3Info info = new ID3.ID3Info(of1.FileName, true); * foreach (AttachedPictureFrame AP in info.ID3v2Info.AttachedPictureFrames.Items) * { * * pictureBox1.Image = Image.FromStream(AP.Data); //添加到PicturBOX内 * * } * * } * goto aa; * aa: * int count = 0; * filepath = of1.FileName; * this.axWindowsMediaPlayer1.URL = of1.FileName; * axWindowsMediaPlayer1.Ctlcontrols.play(); * timer1.Start(); * label2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentItem.name; * * listBox1.Items.Add(of1.FileName); * count++; * listBox1.SelectedIndex = listBox1.Items.Count - 1; * Playbutton.Text = "pause"; * return; * }*/ of1 = new OpenFileDialog(); of1.InitialDirectory = "C://"; of1.Filter = "(音乐文件)*mp3|*mp3"; if (of1.ShowDialog() == DialogResult.OK) { if (of1.FileName != null) { ID3.ID3Info info = new ID3.ID3Info(of1.FileName, true); foreach (AttachedPictureFrame AP in info.ID3v2Info.AttachedPictureFrames.Items) { pictureBox1.Image = Image.FromStream(AP.Data); //添加到PicturBOX内 } goto aa; } aa: int count = 0; filepath = of1.FileName; this.axWindowsMediaPlayer1.URL = of1.FileName; axWindowsMediaPlayer1.Ctlcontrols.play(); timer1.Start(); label2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentItem.name; listBox1.Items.Add(of1.FileName); count++; listBox1.SelectedIndex = listBox1.Items.Count - 1; /*new Thread(() => * { * while (true) * { * try { label3.BeginInvoke(new MethodInvoker(() => label3.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString)); } * catch { } * Thread.Sleep(1000); * } * }) { IsBackground = true }.Start();*/ Playbutton.Text = "pause"; } if (1 == (int)axWindowsMediaPlayer1.playState) { axWindowsMediaPlayer1.Ctlcontrols.play(); } }
private void listView1_DragDrop(object sender, DragEventArgs e) { string tempFilePath = (string)e.Data.GetData(DataFormats.Text); if (!File.Exists(tempFilePath)) { return; } Point Position = new Point(); Position.X = e.X; Position.Y = e.Y; Position = listView1.PointToClient(Position); ID3Info tempID3Info=new ID3Info(tempFilePath,true); string[] tempStrings={tempID3Info.ID3v2Info.GetTextFrame("TRCK"), tempID3Info.ID3v2Info.GetTextFrame("TIT2"), tempID3Info.ID3v2Info.GetTextFrame("TPE1")}; ListViewItem.ListViewSubItem tempSubItem1 = new ListViewItem.ListViewSubItem(); tempSubItem1.Text = tempID3Info.ID3v2Info.GetTextFrame("TRCK"); ListViewItem.ListViewSubItem tempSubItem2 = new ListViewItem.ListViewSubItem(); tempSubItem2.Text = tempID3Info.ID3v2Info.GetTextFrame("TIT2"); ListViewItem.ListViewSubItem tempSubItem3 = new ListViewItem.ListViewSubItem(); tempSubItem3.Text = tempID3Info.ID3v2Info.GetTextFrame("TPE1"); ListViewItem tempItem=new ListViewItem(); tempItem.Tag = tempID3Info; tempItem.SubItems.Add(tempSubItem1); tempItem.SubItems.Add(tempSubItem2); tempItem.SubItems.Add(tempSubItem3); listView1.Items.Add(tempItem); }
/// <summary> /// 保存TAG信息 /// </summary> /// <param name="filePath"></param> public void Save(string filePath) { ID3Info info = new ID3Info(filePath, true); //曲名 info.ID3v2Info.SetTextFrame("TIT2", TrackTitleName); //艺术家 info.ID3v2Info.SetTextFrame("TPE1", ArtistName); ////专辑 info.ID3v2Info.SetTextFrame("TALB",AlbumName ); ////音轨 info.ID3v2Info.SetTextFrame("TRCK", TrackNo); ////碟号 info.ID3v2Info.SetTextFrame("TPOS", DiscNo); ////年份 info.ID3v2Info.SetTextFrame("TYER", SalesYear); info.ID3v2Info.Save(); }