private async Task <(string, IMediaInfo, DateTimeOffset)> ReadMediaInfoAsync( IStorageFile file, bool ignoreDrm) { using (var stream = await file.OpenReadAsync()) { IMediaInfo info = null; int result = -1; // Workaround for mojibake in id3v2 tags: use system code page. if (string.Compare(file.FileType, ".mp3", true) == 0) { if (file is StorageFile) { var props = await((StorageFile)file).Properties.GetMusicPropertiesAsync(); var fInfo = MusicPropertiesMediaInfo.Create(props); result = NativeMethods.GetMediaInfoFromStream(stream, out IMediaInfo extraInfo); fInfo.TrackNumber = extraInfo.TrackNumber; fInfo.TotalTracks = extraInfo.TotalTracks; fInfo.DiscNumber = extraInfo.DiscNumber; fInfo.TotalDiscs = extraInfo.TotalDiscs; fInfo.Date = extraInfo.Date; fInfo.Genre = extraInfo.Genre; fInfo.AllProperties = extraInfo.AllProperties; info = fInfo; } else { result = NativeMethods.GetMediaInfoFromStream(stream, out info); } } else { result = NativeMethods.GetMediaInfoFromStream( stream, out info); } if (result == 0 && info != null) { if (ignoreDrm && !string.IsNullOrWhiteSpace(info.AllProperties["encryption"])) { // Throw DRM exception for upstream reference throw new DrmProtectedException(); } var prop = await file.GetBasicPropertiesAsync(); return(file.Path, info, prop.DateModified); } return(default((string, IMediaInfo, DateTimeOffset))); } }
void FillMetadataBagStub(FileInformation fileInfo, ConcurrentBag <MediaMetadata> bag) { // Prep metadata and cover. IRandomAccessStreamWithContentType stream = null; try { IMediaInfo info; // Workaround for mojibake in id3v2 tags: use system code page. if (string.Compare(fileInfo.FileType, ".mp3", true) == 0) { var fInfo = MusicPropertiesMediaInfo.Create(fileInfo.MusicProperties); IMediaInfo extraInfo; using (stream = fileInfo.OpenReadAsync().AsTask().Result) { var result = NativeMethods.GetMediaInfoFromStream(stream, out extraInfo); } fInfo.TrackNumber = extraInfo.TrackNumber; fInfo.TotalTracks = extraInfo.TotalTracks; fInfo.DiscNumber = extraInfo.DiscNumber; fInfo.TotalDiscs = extraInfo.TotalDiscs; fInfo.Date = extraInfo.Date; fInfo.Genre = extraInfo.Genre; fInfo.AllProperties = extraInfo.AllProperties; info = fInfo; } else { using (stream = fileInfo.OpenReadAsync().AsTask().Result) { var result = NativeMethods.GetMediaInfoFromStream(stream, out info); } } // Ignore all entities with empty title field. if (string.IsNullOrEmpty(info?.Title)) { return; } bag.Add(new MediaMetadata(info, fileInfo.Path, fileInfo.BasicProperties.DateModified)); } catch (Exception ex) { // TODO: Handle exceptions Debug.WriteLine(ex); } }