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

C# UWP MediaPlayerElement-MVVM-System.Net.Http-Authorization-How?,c#,mvvm,uwp,video-streaming,C#,Mvvm,Uwp,Video Streaming,我正在开发一个UWP应用程序,它在XAML中具有MediaPlayerElement。我的页面绑定到MVVM(特别是灯光)视图模型。ViewModel将调用一个服务类,该服务类依次调用一个需要API密钥的URI。该服务将驻留在使用.NET标准2.0的核心库中。因此,我使用的是System.Net.Http,而不是Windows.Web.Http 我想不出来。我试过很多东西 我还尝试重新构建kiewic,使其使用System.Net。对我来说是个大失败 这里主要是基于Windows Templat

我正在开发一个UWP应用程序,它在XAML中具有MediaPlayerElement。我的页面绑定到MVVM(特别是灯光)视图模型。ViewModel将调用一个服务类,该服务类依次调用一个需要API密钥的URI。该服务将驻留在使用.NET标准2.0的核心库中。因此,我使用的是System.Net.Http,而不是Windows.Web.Http

我想不出来。我试过很多东西

我还尝试重新构建kiewic,使其使用System.Net。对我来说是个大失败

这里主要是基于Windows Template Studio项目的工作摘录

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>
.....
VideoService.cs(已编辑)

我明白了:

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.")
所以我一定是错误地穿越了溪流。我希望能得到一些帮助


Keith

我已经测试了你的代码样本,但是我无法用测试Url重现你的问题,你用非授权流测试了吗?@NicoZhu MSFT,很抱歉,测试Url是假的,我应该澄清一下。你是让它使用不同的流,还是我的流失败了?我将视频服务改为呼叫第9频道的视频,但在同一点上仍然失败。请注意,它花了很长时间才收到响应,然后才回调到VM并在那里失败。我将在github和此处更新代码以反映它。我已经使用
Windows.Web.Http
进行了测试,它也会抛出相同的错误(流不支持搜索)。您是否尝试过使用
AdaptiveMediaSource
创建mediasource。我尝试过,但似乎必须使用mp4以外的格式,如HLS和DASH。另外,我相信它是Windows.Web.Http或其他特定于本机Windows的命名空间的一部分,而不是System.Net.Http.Yep,它是设计的,因为
System.Net.Http
用于跨平台,并且它不完全支持UWP平台。
.....
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;
        }
    }
}

.....
Source = MediaSource.CreateFromStream(stream.AsRandomAccessStream(), mediaType);
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.")