Ejemplo n.º 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 path       = track.FormatTitle("%path%");
                    try
                    {
                        path = System.IO.Path.GetDirectoryName(path);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine("Could not get file path for \"" + artistName + "\" / \"" + albumName + "\": " + path);
                        System.Diagnostics.Trace.Indent();
                        System.Diagnostics.Trace.WriteLine(e.Message);
                        System.Diagnostics.Trace.Unindent();
                        continue;                         //skip this one, can't find the path.
                    }

                    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.
                        {
                            mAlbums.Add(new Album(path, artistName, albumName));
                        }
                    }));
                }

                //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));
                }
            }
        }
Ejemplo n.º 2
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));
                }
            }
        }