コード例 #1
0
 /// <summary>
 /// Uses ID3.NET.
 /// </summary>
 /// <param name="filePath"></param>
 /// <param name="stream"></param>
 /// <returns></returns>
 public static SongInfo ReadId3(string filePath, Stream stream)
 {
     try {
         using (var mp3 = new Mp3Stream(stream)) {
             var tags = new List <Id3Tag>(mp3.GetAllTags());
             if (tags.Count > 0)
             {
                 var firstTag = tags.First();
                 var title    = firstTag.Title.Value;
                 if (String.IsNullOrWhiteSpace(title))
                 {
                     title = PathHelper.Instance.FileNameWithoutExtension(filePath);
                 }
                 // All ID3 libraries for WP suck; no way to get duration reliably prior to opening in a player,
                 // so sets duration to 0 for now and obtain it when needed for playback.
                 return(new SongInfo(title, firstTag.Artists.Value, firstTag.Album.Value, TimeSpan.FromSeconds(0)));
             }
             else
             {
                 return(null);
             }
         }
     } catch (IndexOutOfRangeException) {
         // maybe thrown if the file is cocked up and id3 tags cannot be read
         return(null);
     }
 }
コード例 #2
0
ファイル: UnitTest1.cs プロジェクト: malliina/musicpimp-win
 private void testId3(string filePath)
 {
     if (!File.Exists(filePath))
     {
         Debug.WriteLine(filePath + " does not exist");
         return;
     }
     else
     {
         Debug.WriteLine(filePath + " exists");
     }
     using (var file = new FileStream(filePath, FileMode.Open)) {
         using (var mp3 = new Mp3Stream(file)) {
             //var t = mp3.GetTag(Id3TagFamily.Version2x);
             //Debug.WriteLine(t.Title.Value);
             var tags     = new List <Id3Tag>(mp3.GetAllTags());
             var tagCount = tags.Count;
             tags.ForEach(tag => {
                 Debug.WriteLine(tag.Title.Value);
             });
             Debug.WriteLine("tags: " + tagCount);
             Debug.WriteLine("duration: " + mp3.Audio.Duration.TotalSeconds);
             Debug.WriteLine("bitrate: " + mp3.Audio.Bitrate);
             Assert.AreEqual(506, mp3.Audio.Duration);
         }
     };
 }
コード例 #3
0
ファイル: TagsHandler.cs プロジェクト: justdude/VKLoader
        public static void Read(string path, Sound sound)
        {
            if (!File.Exists(path))
                return;

            File.SetAttributes(path, FileAttributes.Normal);

            try
            {
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                Mp3Stream mp3 = new Mp3Stream(stream);

                if (!mp3.HasTags)
                {
                    stream.Close();
                    ComputeFromFileName(path, sound);
                    return;
                }

                //mp3.GetAudioStream().
                var tags = mp3.GetAllTags();

                foreach (var tag in tags)
                    if (tag != null)
                    {
                        if (string.IsNullOrWhiteSpace(tag.Artists.Value) && string.IsNullOrWhiteSpace(tag.Title.Value))
                        {
                            ComputeFromFileName(path, sound);
                        }
                        else
                        {
                          byte[] input = null;

                          if (!string.IsNullOrWhiteSpace(tag.Title.Value))
                          {
                              input = tag.Title.Encode();
                              sound.title = ConvertEncoding(tag.Artists.Value, input, Id3TextEncoding.Iso8859_1);
                          }

                          if (!string.IsNullOrWhiteSpace(tag.Artists))
                          {
                              input = tag.Artists.Encode();
                              sound.artist = ConvertEncoding(tag.Artists.Value, input, Id3TextEncoding.Iso8859_1);
                          }
                        }

                        GetPicture(tag);
                        //int.TryParse(tag.BeatsPerMinute.Value, out modelView.duration);
                        break;
                    }
                stream.Close();

            }//try
            catch (Exception)
            {
                ComputeFromFileName(path, sound);
            }
        }
コード例 #4
0
        private async Task <BitmapImage> GetAlbumImage(Stream path)
        {
            BitmapImage bitmapImage = new BitmapImage(new Uri("ms-appx:///Image/DefaultImage.png", UriKind.RelativeOrAbsolute));
            var         mp3         = new Mp3Stream(path);
            var         tags        = mp3.GetAllTags();
            var         picture     = tags[0].Pictures[0].PictureData;

            if (picture.Any())
            {
                using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(picture.AsBuffer());

                    stream.Seek(0);
                    await bitmapImage.SetSourceAsync(stream);
                }
            }
            return(bitmapImage);
        }
コード例 #5
0
ファイル: TestModel.cs プロジェクト: malliina/musicpimp-win
 public void ReadID3(Stream stream)
 {
     try {
         using (var mp3 = new Mp3Stream(stream)) {
             var tags = new List <Id3Tag>(mp3.GetAllTags());
             if (tags.Count > 0)
             {
                 var firstTag = tags.First();
                 var title    = firstTag.Title.Value;
                 if (String.IsNullOrWhiteSpace(title))
                 {
                     title = "crazy";
                 }
                 //return new SongInfo(title, firstTag.Artists.Value, firstTag.Album.Value);
             }
         }
     } catch (IndexOutOfRangeException iore) {
         // maybe thrown if the file is cocked up and id3 tags cannot be read
         Debug.WriteLine(iore.Message);
     }
 }
コード例 #6
0
        public Dictionary<string, string> GetMetaData(string path)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            path = path.Replace("|", "\\");
            using (var stream = new FileStream(path, FileMode.Open))
            {
                Mp3Stream m = new Mp3Stream(stream);
                if (m.HasTags)
                {
                    var tag = m.GetAllTags();

                    list.Add("Artist", tag[0].Artists.Value);
                    list.Add("Album", tag[0].Album.Value);
                    list.Add("Genre", GetGenre(tag[0].Genre.Value));
                    list.Add("Title", tag[0].Title.Value);
                    list.Add("Track", tag[0].Track.Value);
                }
            }
            return list;
        }
コード例 #7
0
ファイル: TagsHandler.cs プロジェクト: justdude/VKLoader
        public static void Read(string path, ModelView.SoundModelView modelView)
        {
            if (!File.Exists(path))
                return;

            File.SetAttributes(path, FileAttributes.Normal);

            try
            {
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                Mp3Stream mp3 = new Mp3Stream(stream);

                if (!mp3.HasTags)
                {
                    stream.Close();
                    ComputeFromFileName(path, modelView.Sound);
                    return;
                }

                var tags = mp3.GetAllTags();

                foreach (var tag in tags)
                    if (tag != null)
                    {
                        modelView.Sound.artist = tag.Artists.Value;
                        modelView.Sound.title = tag.Title.Value;
                        modelView.Photo = GetPicture(tag);
                        //int.TryParse(tag.BeatsPerMinute.Value, out modelView.duration);
                        break;
                    }
                stream.Close();

            }//try
            catch (Exception)
            {
                ComputeFromFileName(path, modelView.Sound);
            }
        }