Example #1
0
        /// <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);
        }
Example #2
0
        /// <summary>
        /// 获取MP3文件
        /// </summary>
        private void GetMP3File()
        {
            DateTime dt1 = DateTime.Now;
            FileDirectoryFinder finder = new FileDirectoryFinder();
            this.isScanMP3End = false;
            MP3FileData mp3File = new MP3FileData();
            bool isQueueEmpty = false;
            while (true)
            {
                //如果用户执行了停止索引,结束。
                if (this.isStopIndexMP3 == true)
                    break;
                lock (this.mp3DirectoryStack)
                {
                    if (this.mp3DirectoryStack.Count > 0)
                    {
                        isQueueEmpty = false;
                        finder.SearchPath = this.mp3DirectoryStack.Pop();
                        finder.Reset();
                    }
                    else
                    {
                        isQueueEmpty = true;
                    }
                }
                if (isQueueEmpty == false)
                {
                    while (finder.MoveNext())
                    {
                        //当前对象为目录
                        if (finder.IsFile == true)
                        {
                            //不是mp3文件,继续。
                            if (finder.CurrentFileData.name.ToLower().EndsWith(".mp3") == false)
                            {
                                continue;
                            }

                            lock (this.mp3FileQueue)
                            {
                                mp3File.name = finder.CurrentFileData.name;
                                mp3File.directory = finder.CurrentFileData.directory;

                                mp3File.length = Deal.ToLong(finder.CurrentFileData.nFileSizeHigh,
                                        finder.CurrentFileData.nFileSizeLow).ToString();

                                mp3File.lastWriteTime = Deal.ToDateTimeString(
                                    finder.CurrentFileData.ftLastWriteTime_dwHighDateTime,
                                   finder.CurrentFileData.ftLastWriteTime_dwLowDateTime);

                                lock (this.mp3FileQueue)
                                {
                                    this.mp3FileQueue.Enqueue(mp3File);
                                }
                                this.mp3FileCounter++;
                            }
                        }
                        else
                        {
                            lock (this.mp3DirectoryStack)
                            {
                                this.mp3DirectoryStack.Push(finder.FullName);
                            }
                            this.mp3FolderCounter++;
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            double d = (DateTime.Now - dt1).TotalSeconds;
            //Console.Out.WriteLine("GetMP3File" + d);
            this.isScanMP3End = true;
        }