Ejemplo n.º 1
0
        /// <summary>
        /// Returns a cover art image for the specified track.
        /// </summary>
        /// <param name="trackInfo">
        /// An object contating data of currently playing track.
        /// </param>
        /// <param name="concreteRule">
        /// A rule that should be used to search for cover art image.
        /// </param>
        /// <returns>Result of cover art image search.</returns>
        public Bitmap GetBitmap(TrackInfo trackInfo, FindRule concreteRule)
        {
            Bitmap result = null;

            var fileDir = Path.GetDirectoryName(trackInfo.FileName);

            if (String.IsNullOrEmpty(fileDir))
            {
                return null;
            }

            var dir = new DirectoryInfo(fileDir);

            String searchPattern;

            if (concreteRule.Rule == CoverRuleType.AlbumFile)
            {
                searchPattern = trackInfo.Album + ".*";
            }
            else if (concreteRule.Rule == CoverRuleType.CoverFile)
            {
                searchPattern = "cover.*";
            }
            else
            {
                throw new NotSupportedException("The " + concreteRule.Rule + " rule cannot be handled by this method.");
            }

            // In case the pattern is not correct for the file system, do not proceed further.
            if (Core.Algorithms.ContainsInvalidFileNameChars(searchPattern))
            {
                return null;
            }

            var covers = dir.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

            if (covers.Length > 0)
            {
                foreach (var cover in covers)
                {
                    Debug.WriteLine(String.Format("Loading image from HDD : {0}", cover.Name));

                    var ms = new MemoryStream(File.ReadAllBytes(cover.FullName));
                    try
                    {
                        result = (Bitmap)Image.FromStream(ms);
                        break;

                    }
                    catch (ArgumentException)
                    {
                        // Means that this is not an image or this format is not supported.
                        ms.Dispose();
                    }
                }
            }

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Being in a separate thread, this method uses find rules one by one 
        /// and tries to load the cover image.
        /// </summary>
        /// <param name="initialRequestId">An identifier of current search session.</param>
        private Bitmap LoadImageWorkItem(Object initialRequestId, CancellationToken token)
        {
            Bitmap result = null;

            // Make a pre-check that this request is really needed.
            if ((Guid)initialRequestId != _currentRequestId)
            {
                return null;
            }

            try
            {
                _logger.Write(string.Format("Album: {0};\tArtist: {1};\tTrack: {2}", _aimpPlayer.CurrentFileInfo.Album, _aimpPlayer.CurrentFileInfo.Artist, _aimpPlayer.CurrentFileInfo.FileName));

                // This object prevents race conditions when the search 
                // is in progress and at the same time AIMP is changing a track.
                var trackInfo = new TrackInfo(_aimpPlayer);

                var enabledRules = Config.Instance.Rules.Where(r => r.Enabled).ToArray();

                for (Int32 i = 0; i < enabledRules.Length; i++)
                {
                    if (token.IsCancellationRequested)
                    {
                        return null;
                    }

                    FindRule rule;
                    String moduleName;

                    // If it is a local audio file - use ordinary set of rules.
                    if (!trackInfo.IsStream)
                    {
                        rule = enabledRules.Skip(i).FirstOrDefault();
                        Contract.Assume(rule != null);
                        moduleName = rule.Module;
                    }
                     //If it is a CDA or a radio station - only LastFm can help us.
                    else
                    {
                        // If Last.Fm is disabled - stop iterating, we can't get the cover image.
                        if (!enabledRules.Any(r => r.Module == LastFmFinder.ModuleName))
                        {
                            break;
                        }

                        i = enabledRules.Length; // Prevent further iterations.

                        rule = null;
                        moduleName = LastFmFinder.ModuleName;
                    }

                    ICoverFinder finder = CoverModules.FirstOrDefault(c => c.Name == moduleName);
                    if (finder == null)
                    {
                        throw new ApplicationException("Finder plugin " + moduleName + " has not been found.");
                    }

                    result = finder.GetBitmap(_aimpPlayer, rule);

                    if (result != null)
                    {
                        _logger.Write(string.Format("Module: {0}\tCover art has been found", moduleName));
                        break;
                    }
                    {
                        _logger.Write(string.Format("Module: {0}\tCover art has not been found", moduleName));
                    }
                }

                _logger.Write("------------------------------------------------------");
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show(
                    LocalizedData.ErrorOnCoverImageSearch + ex.Message,
                    LocalizedData.PluginName,
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
#endif
            }

            return result;
        }