/// <summary> /// 分析MP3Tag并加入索引 /// </summary> private void AnalyzeMP3Tag() { DateTime dt1 = DateTime.Now; MP3TagData mp3Tag = new MP3TagData(); bool isQueueEmpty = true; while (true) { //如果用户执行了停止索引,结束。 if (this.isStopIndexMP3 == true) { break; } lock (this.mp3TagQueue) { if (this.mp3TagQueue.Count > 0) { isQueueEmpty = false; mp3Tag = this.mp3TagQueue.Dequeue(); } else { isQueueEmpty = true; } } if (isQueueEmpty == false) { //设置当前正在索引的文件夹 lock (this.currentDirectory) { this.currentDirectory = mp3Tag.directory; } //收集音乐家 if (this.mp3Artist.Contains(mp3Tag.artist) == false) { this.mp3Artist.Add(mp3Tag.artist); Console.WriteLine(mp3Tag.artist); } //收集文件夹 if (this.mp3Directorys.Contains(mp3Tag.directory) == false) { this.mp3Directorys.Add(mp3Tag.directory); } this.f_Name.SetValue(mp3Tag.name); this.f_Length.SetValue(mp3Tag.length); this.f_Directory.SetValue(mp3Tag.directory); this.f_LastWriteTime.SetValue(mp3Tag.lastWriteTime); this.m_Album.SetValue(mp3Tag.album); this.m_Artist.SetValue(mp3Tag.artist); this.m_Genre.SetValue(mp3Tag.genre); this.m_PubYear.SetValue(mp3Tag.pubYear); this.m_SongName.SetValue(mp3Tag.songName); this.m_ArtistStore.SetValue(mp3Tag.artist); Document doc = new Document(); doc.Add(this.f_Name); doc.Add(this.f_Length); doc.Add(this.f_Directory); doc.Add(this.f_LastWriteTime); doc.Add(this.m_Album); doc.Add(this.m_Artist); doc.Add(this.m_Genre); doc.Add(this.m_PubYear); doc.Add(this.m_SongName); doc.Add(this.m_ArtistStore); this.mp3IndexWriter.AddDocument(doc); } else if (this.isGetMP3TagEnd == false) { //Console.Out.WriteLine("mp3TagQueue 空"); Thread.Sleep(0);//让出时间片给其他线程 continue; } else { break; } } //对索引优化,将多个段文件合并成一个文件,减少搜索时打开的文件数,提高搜索速度。 this.mp3IndexWriter.Optimize(); this.mp3IndexWriter.Close(); this.dtMP3Stop = DateTime.Now;//MP3索引结束 this.isIndexMP3Complete = true; double d = (DateTime.Now - dt1).TotalSeconds; //Console.Out.WriteLine("分析MP3" + d); //Console.Out.WriteLine(this.mp3FileCounter + this.mp3FolderCounter); //执行索引后可能修改mp3Artist,更新mp3Artist。 Static.MP3Artist = this.mp3Artist; //执行索引后可能修改mp3Artist,置MP3ArtistInfo为null,在搜索时重新更新。 Static.MArtistInfo = null; this.SaveIndexInfo();//保存索引信息 this.CallBack(); }
/// <summary> /// 获取MP3的Tag信息 /// </summary> private void GetMP3Tag() { DateTime dt1 = DateTime.Now; this.isGetMP3TagEnd = false; string fullName = "";//文件全名 MP3FileData mp3File = new MP3FileData(); MP3TagData mp3Tag = new MP3TagData(); bool isQueueEmpty = false; while (true) { //如果用户执行了停止索引,结束。 if (this.isStopIndexMP3 == true) { break; } lock (this.mp3FileQueue) { if (this.mp3FileQueue.Count > 0) { isQueueEmpty = false; mp3File = this.mp3FileQueue.Dequeue(); } else { isQueueEmpty = true; } } if (isQueueEmpty == false) { //文件信息 mp3Tag.name = mp3File.name; mp3Tag.length = mp3File.length; mp3Tag.directory = mp3File.directory; mp3Tag.lastWriteTime = mp3File.lastWriteTime; fullName = mp3Tag.directory + mp3Tag.name; //读取MP3文件的最后128个字节的内容 try { using (FileStream fs = new FileStream(fullName, FileMode.Open, FileAccess.Read)) { fs.Seek(-128, SeekOrigin.End); fs.Read(tagBody, 0, 128); fs.Close(); } } catch { continue; } //取TAG段的前三个字节 tagFlag = Encoding.Default.GetString(tagBody, 0, 3); //如果没有TAG信息,则置为null if (!"TAG".Equals(tagFlag, StringComparison.InvariantCultureIgnoreCase)) { mp3Tag.album = ""; mp3Tag.artist = ""; mp3Tag.genre = ""; mp3Tag.pubYear = ""; mp3Tag.songName = ""; } else { //按照MP3 ID3 V1 的tag定义,依次读取相关的信息 mp3Tag.songName = Encoding.Default.GetString(tagBody, 3, 30); mp3Tag.songName = (mp3Tag.songName.Trim()).Trim('\0'); mp3Tag.artist = Encoding.Default.GetString(tagBody, 33, 30); mp3Tag.artist = (mp3Tag.artist.Trim()).Trim('\0'); mp3Tag.album = Encoding.Default.GetString(tagBody, 62, 30); mp3Tag.album = (mp3Tag.album.Trim()).Trim('\0'); mp3Tag.pubYear = Encoding.Default.GetString(tagBody, 93, 4); mp3Tag.pubYear = (mp3Tag.pubYear.Trim()).Trim('\0'); Int16 g = (Int16)tagBody[127]; mp3Tag.genre = g >= this.GENRE.Length ? "" : this.GENRE[g]; } lock (this.mp3TagQueue) { this.mp3TagQueue.Enqueue(mp3Tag); } } else if (this.isScanMP3End == false) { //Console.WriteLine("mp3FileQueue 空"); Thread.Sleep(0);//让出时间片给其他线程 continue; } else { break; } } this.isGetMP3TagEnd = true;//获取MP3Tag结束 double d = (DateTime.Now - dt1).TotalSeconds; //Console.Out.WriteLine("GetMP3TagEnd" + d); }