Pavan Tiwari
Pavan Tiwari

Reputation: 3187

Setting thumbnail of SystemMediaTransportControlsDisplayUpdater

I am Setting thumbnail of SMTC control from stream via SystemMediaTransportControlsDisplayUpdater, but it is not working

    var response = await new HttpClient().GetAsync(imgUrl);
systemMediaTransportControlsDisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromStream((await response.Content.ReadAsStreamAsync()).AsRandomAccessStream());

Same is working if i use Url to create random stream.

systemMediaTransportControlsDisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(imgUrl));

I am merging two images and i want to assign that image as thumbnai to SMTC. To merge images below code i am using.

 var response = await httpClient.GetAsync(imgUrl);
                var writeableBmp = new WriteableBitmap(1, 1);
                var image1 = await writeableBmp.FromStream(await response.Content.ReadAsStreamAsync());
                var image2 = await writeableBmp.FromContent(imgUrl1);

                image1.Blit(new Rect(0, 0, image1.PixelWidth, image1.PixelHeight), image2, new Rect(0, 0, image2.PixelWidth, image2.PixelHeight));
                var randomAccessStream = image1.PixelBuffer.AsStream().AsRandomAccessStream();
systemMediaTransportControlsDisplayUpdater.Thumbnail = 
 = RandomAccessStreamReference.CreateFromStream(randomAccessStream );

What exactly wrong with merging or setting thumbnail to SMTC? Thanks

Upvotes: 0

Views: 246

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

stream via SystemMediaTransportControlsDisplayUpdater, but it is not working

I could reproduce your issue, I will report this, currently there is a workaround that converter WriteableBitmap to StorageFile then pass file argument to RandomAccessStreamReference.CreateFromFile method. You could refer the following code,

private async void MediaPlayer_MediaOpened(MediaPlayer sender, object args)
{
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        var imgUrl = new Uri("https://image.diyidan.net/post/2015/10/27/LILeAOb3BF8qR8Uz.png");
        var imgUrl1 = new Uri("ms-appx:///Assets/test.png");
        var httpclient = new HttpClient();
        var response = await httpclient.GetAsync(imgUrl);

        var writeableBmp = new WriteableBitmap(1, 1);
        var image1 = await writeableBmp.FromStream(await response.Content.ReadAsStreamAsync());
        var image2 = await writeableBmp.FromContent(imgUrl1);


        image1.Blit(new Rect(0, 0, image1.PixelWidth, image1.PixelHeight), image2, new Rect(0, 0, image2.PixelWidth, image2.PixelHeight));

        var file = await WriteableBitmapToStorageFile(image1, FileFormat.Png);

        SystemMediaTransportControlsDisplayUpdater updater = sender.SystemMediaTransportControls.DisplayUpdater;
        updater.Thumbnail = RandomAccessStreamReference.CreateFromFile(file);
        updater.Update();
    });

}
private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap WB, FileFormat fileFormat)
{
    string FileName = "YourFile.";
    Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
    switch (fileFormat)
    {
        case FileFormat.Jpeg:
            FileName += "jpeg";
            BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
            break;
        case FileFormat.Png:
            FileName += "png";
            BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
            break;
        case FileFormat.Bmp:
            FileName += "bmp";
            BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
            break;
        case FileFormat.Tiff:
            FileName += "tiff";
            BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
            break;
        case FileFormat.Gif:
            FileName += "gif";
            BitmapEncoderGuid = BitmapEncoder.GifEncoderId;
            break;
    }
    var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
        Stream pixelStream = WB.PixelBuffer.AsStream();
        byte[] pixels = new byte[pixelStream.Length];
        await pixelStream.ReadAsync(pixels, 0, pixels.Length);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)WB.PixelWidth, (uint)WB.PixelHeight,
            96.0,
            96.0,
            pixels);
        await encoder.FlushAsync();
    }
    return file;
}
private enum FileFormat
{
    Jpeg,
    Png,
    Bmp,
    Tiff,
    Gif
}

Upvotes: 0

Related Questions