예제 #1
0
        private void ReadFoobarWorker()
        {
            try
            {
                Foobar2000.Tracks07 tracks = SelectedPlaylist.GetTracks(null);
                //Now searching for something, so set the state to indicate that.
                //Also set the count of albums, for the progress bar
                Dispatcher.Invoke(DispatcherPriority.DataBind, new ThreadStart(delegate
                {
                    State        = BrowserState.FindingFiles;
                    Progress     = 0;
                    ProgressMax  = tracks.Count;
                    ProgressText = "Reading Media Library...";
                }));
                foreach (Foobar2000.Track07 track in tracks)
                {
                    string artistName = track.FormatTitle("%album artist%");
                    string albumName  = track.FormatTitle("%album%");
                    string filename   = track.FormatTitle("%path%");
                    string path;
                    try
                    {
                        path = System.IO.Path.GetDirectoryName(filename);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine("Could not get file path for \"" + artistName + "\" / \"" + albumName + "\": " + filename);
                        System.Diagnostics.Trace.Indent();
                        System.Diagnostics.Trace.WriteLine(e.Message);
                        System.Diagnostics.Trace.Unindent();
                        continue;                         //skip this one, can't find the path.
                    }

                    var  album      = new Album(path, artistName, albumName);
                    bool addedAlbum = false;

                    Dispatcher.Invoke(DispatcherPriority.DataBind, new ThreadStart(delegate
                    {
                        Progress++;
                        if (!(String.IsNullOrEmpty(artistName) && String.IsNullOrEmpty(albumName)))                         //No point adding it if no artist or album could be found.
                        {
                            addedAlbum = mAlbums.Add(album);
                        }
                    }));

                    if (addedAlbum)
                    {
                        // Check for embedded art
                        int?        embeddedArtIndex = null;
                        TagLib.File fileTags         = null;
                        try
                        {
                            fileTags         = TagLib.File.Create(filename, TagLib.ReadStyle.None);
                            embeddedArtIndex = EmbeddedArtHelpers.GetEmbeddedFrontCoverIndex(fileTags);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Trace.WriteLine("TagLib# could not get embedded artwork for file: " + filename);
                            System.Diagnostics.Trace.Indent();
                            System.Diagnostics.Trace.WriteLine(e.Message);
                            System.Diagnostics.Trace.Unindent();
                            //If embedded images couldn't be read, ignore that
                        }
                        finally
                        {
                            if (fileTags != null)
                            {
                                fileTags.Mode = TagLib.File.AccessMode.Closed;
                            }
                        }

                        if (embeddedArtIndex.HasValue)
                        {
                            //Read the picture from the data
                            album.SetArtFile(EmbeddedArtHelpers.GetEmbeddedFilePath(filename, embeddedArtIndex.Value));
                        }
                    }
                }

                //Finished with the FindingFiles state, so now set the state to whatever the results state is (either FindingArt, or Done).
                Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new ThreadStart(delegate
                {
                    ProgressText = mResults.ProgressText;
                    State        = mResults.State;
                }));
            }
            catch (ThreadAbortException)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new ThreadStart(delegate
                {
                    State = BrowserState.Stopped;
                }));
            }
            catch (Exception e)
            {
                uint hResult = (uint)System.Runtime.InteropServices.Marshal.GetHRForException(e);

                if (e is COMException ||
                    (hResult == 0x800706BE ||                    //RPC failed
                     hResult == 0x80004002))                     //No interface
                {
                    SetErrorState("Lost connection to Foobar automation server while reading media library");
                }
                else
                {
                    SetErrorState(String.Format("Error occurred while reading media library: {0}", e.Message));
                }
            }
        }
예제 #2
0
        /// <summary>Reads the album and artist data from specified media file or folder. Adds it to the <see cref="mAlbums"/> collection, and if it does so, also adds it to <paramref name="addedAlbums"/>.</summary>
        /// <param name="filePathPattern">Use null to use the ID3 tags instead</param>
        /// <returns>The <see cref="Album"/> that was added to <see cref="mAlbums"/>, or <c>null</c> if none could be read.</returns>
        private void ReadMediaFile(FileSystemInfo file, Regex filePathPattern, IList <Album> addedAlbums)
        {
            if (file is DirectoryInfo && filePathPattern == null)             //If a DirectoryInfo is used, then the filePathPattern must have ended in \.
            {
                throw new ArgumentException("Directories are only supported for pattern matching, not ID3 tags", "file");
            }

            Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new ThreadStart(delegate
            {
                ProgressText = "Searching... " + file.Name;
            }));

            string artistName       = null;
            string albumName        = null;
            int?   embeddedArtIndex = null;

            if (filePathPattern == null)
            {
                //Read ID3 Tags
                TagLib.File fileTags = null;
                try
                {
                    fileTags = TagLib.File.Create(file.FullName, TagLib.ReadStyle.None);
                    if (fileTags.Tag.AlbumArtists.Length == 0)
                    {
                        artistName = String.Join(" / ", fileTags.Tag.Performers);
                    }
                    else
                    {
                        artistName = String.Join(" / ", fileTags.Tag.AlbumArtists);
                    }
                    albumName = fileTags.Tag.Album;

                    embeddedArtIndex = EmbeddedArtHelpers.GetEmbeddedFrontCoverIndex(fileTags);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.WriteLine("TagLib# could not get artist and album information for file: " + file.FullName);
                    System.Diagnostics.Trace.Indent();
                    System.Diagnostics.Trace.WriteLine(e.Message);
                    System.Diagnostics.Trace.Unindent();
                    return;                     //If this media file couldn't be read, just go on to the next one.
                }
                finally
                {
                    if (fileTags != null)
                    {
                        fileTags.Mode = TagLib.File.AccessMode.Closed;
                    }
                }
            }
            else
            {
                //Read from file path
                Match match = filePathPattern.Match(file.FullName);
                if (match.Success)
                {
                    artistName = match.Groups["artist"].Value;
                    albumName  = match.Groups["album"].Value;
                }
            }

            if (!(String.IsNullOrEmpty(artistName) && String.IsNullOrEmpty(albumName)))             //No point adding it if no artist or album could be found.
            {
                string basePath;
                if (file is FileInfo)
                {
                    basePath = ((FileInfo)file).DirectoryName;
                }
                else
                {
                    System.Diagnostics.Debug.Assert(file is DirectoryInfo, "Expecting file to be one of FileInfo or DirectoryInfo");
                    basePath = ((DirectoryInfo)file).FullName;
                }

                Album album = new Album(basePath, artistName, albumName);
                if (embeddedArtIndex.HasValue)
                {
                    //Read the picture from the data
                    album.SetArtFile(EmbeddedArtHelpers.GetEmbeddedFilePath(file.FullName, embeddedArtIndex.Value));
                }

                Dispatcher.Invoke(DispatcherPriority.DataBind, new ThreadStart(delegate
                {
                    if (mAlbums.Add(album))
                    {
                        addedAlbums.Add(album);
                    }
                }));
            }
        }