UWP MediaPlayerElement - MVVM - System.Net.Http - Authorization - How?

Viewed 176

I am working on a UWP App that has MediaPlayerElement in XAML. My page binds to a MVVM (Light specifically) ViewModel. The ViewModel will be calling a service class that in turns calls a URI that requires a API Key. The Service will be residing in a Core library using .NET Standard 2.0. Therefore, I am using System.Net.Http rather than Windows.Web.Http.

I cannot figure this out. I've tried a number of items.

I also tried to rearchitect kiewic's MediaPlayerElmentWithHttpClient to use System.Net. Big failure for me.

Essentially here are excerpts of the work based off a Windows Template Studio project.

MediaPlayerPage.xaml

.....
    <MediaPlayerElement
        x:Name="mpe"
        AreTransportControlsEnabled="True"
        AutoPlay="True"
        Source="{x:Bind ViewModel.Source, Mode=OneWay}" >
        <MediaPlayerElement.TransportControls>
            <MediaTransportControls IsCompact="False" />
        </MediaPlayerElement.TransportControls>
    </MediaPlayerElement>
.....

The MediaPlayerViewModel.cs

.....
    private IMediaPlaybackSource _source;
    public IMediaPlaybackSource Source
    {
        get { return _source; }
        set { Set(ref _source, value); }
    }
.....
    public MediaPlayerViewModel()
    {
        RequestVideos();
    }

    private async Task RequestVideos()
    {
        var videoService = new VideoService();
        Stream stream = await videoService.GetVideoAsync();

        // Possibly the problem is hear as the stream has to be sent as a RandomAccess Stream to the control.
        Source = MediaSource.CreateFromStream(stream.AsRandomAccessStream(), mediaType);
        PosterSource = DefaultPoster;
    }

The VideoService.cs (EDITED)

.....
public class VideoService
{
    private const string DefaultSource = "https://sec.ch9.ms/ch9/db15/43c9fbed-535e-4013-8a4a-a74cc00adb15/C9L12WinTemplateStudio_high.mp4";

    public async Task<Stream> GetVideoAsync()
    {
        string videoPath = @"https://<videodomain>.com/something720.mp4";
        string providerKeyName = "Provider-Api-Key";
        string providerKeyValue = "123456";
        bool authRequired = false;

       // var uri = new Uri(videoPath);
        var client = new HttpClient();
        HttpRequestMessage request = null;
        if (authRequired)
        {
            request = new HttpRequestMessage(HttpMethod.Get, new Uri(videoPath));
            request.Headers.Add(providerKeyName, providerKeyValue);
        }
        else
        {
            request = new HttpRequestMessage(HttpMethod.Get, new Uri(DefaultSource));
        }

        HttpResponseMessage response = await client.SendAsync(request);//, HttpCompletionOption.ResponseContentRead);

        //The issue must be here or back on the ViewModel.
        using(var stream = await response.Content.ReadAsStreamAsync())
        {
            return stream;
        }
    }
}

.....

Full code found at github

From looking at the Diagnostic tools, an exception occurrs at the ViewModel's:

Source = MediaSource.CreateFromStream(stream.AsRandomAccessStream(), mediaType);

I get this:

Activated Historical Code Context   
Exception thrown: 'System.NotSupportedException' in System.Runtime.WindowsRuntime.dll 
("Cannot use the specified Stream as a Windows Runtime IRandomAccessStream because 
this Stream does not support seeking.") Exception thrown: 'System.NotSupportedException'
 in System.Runtime.WindowsRuntime.dll ("Cannot use the specified Stream as a 
Windows Runtime IRandomAccessStream because this Stream does not support seeking.")

So I must be crossing the streams incorrectly. I would appreciate some help.

Keith

0 Answers
Related