/// <summary> /// Tries to loads multiple files at once using the a single handler's <see cref="MediaTypeHandler.HandleMultiple"/> method /// in order to load them into a single media object if supported. This assumes that the files exist. /// </summary> /// <param name="uris">The URIs to load.</param> /// <returns>The loaded media objects, either one per file or less.</returns> public static IEnumerable <Media> LoadMultipleMediaMetadata(IEnumerable <Uri> uris) { MediaTypeHandler maxPriorityHandler = handlers.First(); int maxPriority = maxPriorityHandler.TestMultiple(uris); foreach (var h in handlers.Skip(1)) { int priority = h.TestMultiple(uris); if (priority > maxPriority) { maxPriorityHandler = h; maxPriority = priority; } } if (maxPriority < 0) { // not all of them are supported by a single handler => load them separately return(uris.Select(u => LoadMediaMetadata(u, null))); } else { var result = maxPriorityHandler.HandleMultiple(uris); foreach (var m in result) { m.LoadMetadataHelper(); } return(result); } }
/// <summary> /// Registers file handlers from the given types. Any non-abstract subclass of <see cref="MediaFileHandler"/> /// that is found will be registered. /// </summary> /// <param name="types"></param> public static void RegisterHandlersFromTypes(IEnumerable <Type> types) { foreach (var type in types) { if (type.IsClass && !type.IsAbstract && typeof(MediaTypeHandler).IsAssignableFrom(type)) { MediaTypeHandler handler = (MediaTypeHandler)Activator.CreateInstance(type); handlers.Add(handler); } } }
/// <summary> /// Creates a media object from a URI. /// If the URI does not reference an HTTP resource, a <see cref="FileNotFoundMedia"/> is returned if /// the resource does not exist. /// If no appropiate file handler is found, a <see cref="UnsupportedMedia"/> is returned. /// Otherwise the correct <see cref="Media"/> type is returned. /// </summary> /// <param name="uri">The URI to load from. This can either be local (file://), remote (http://) /// or a reference to the song database (song://).</param> /// <returns>A <see cref="Media"/> object.</returns> public static Media LoadMediaMetadata(Uri uri, Dictionary <string, string> options) { try { MediaTypeHandler maxPriorityHandler = handlers.First(); int maxPriority = maxPriorityHandler.Test(uri); foreach (var h in handlers.Skip(1)) { int priority = h.Test(uri); if (priority > maxPriority) { maxPriorityHandler = h; maxPriority = priority; } } if (maxPriority < 0) { return(new UnsupportedMedia(uri)); } else { var result = maxPriorityHandler.Handle(uri, options); result.LoadMetadataHelper(); return(result); } } catch (FileNotFoundException) { return(new FileNotFoundMedia(uri)); } catch (DirectoryNotFoundException) { return(new FileNotFoundMedia(uri)); } }