Search code examples
c#xamluwpmedia-playervideo-player

uwp converting StorageItemThumbnail to RandomAccessStreamReference


as you can see in the following code

var mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(myVideoFile));
MediaItemDisplayProperties props = mediaPlaybackItem.GetDisplayProperties();
props.Type = Windows.Media.MediaPlaybackType.Video;
props.VideoProperties.Title = CurrentVideo.MyVideoFile.DisplayName;
props.Thumbnail = (await CurrentVideo.MyVideoFile.GetThumbnailAsync(ThumbnailMode.VideosView));
mediaPlaybackItem.ApplyDisplayProperties(props);

in my uwp app I am trying to set the DisplayProperties to be shown in systemmediatransportcontrols of my mediaplaybackitem. I am able to set other properties, but when I try to set the thumbnail, I am getting a StorageItemThumbnail object from GetThumbnailAsync and I want to assign that to props.Thumbnail which is of type RandomAccessStreamReference so I am wondering how can I convert it to the required type.

systemmedia transport controls do have a method to update the thumbnail by copying the metadata automatically from the file. but in my scenario I am updating the display properties with my mediaplaybackitem the docs do show how to update the title as I am doing here, but they don't show how to update the thumbnail. also when the media opens I don't have acces to storagefile object, that is why I want to set the display properties on the mediaPlayBackItem object so it can manage automatically whenever the source of my media changes.


Solution

  • var thumb = await CurrentVideo.MyVideoFile.GetThumbnailAsync(ThumbnailMode.VideosView); props.Thumbnail = RandomAccessStreamReference.CreateFromStream(thumb);

    For what it's worth, as Thumbnails are actual RandomAccessStreams it's best to dispose of them when you're finished with them.