小编Cri*_*van的帖子

如何在源为https uri时播放wpf MediaElement

在wpf独立应用程序(.exe)中,我在MainWindow中包含了一个MediaElement

<Window x:Class="Media.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main Window" Height="350" Width="525">
    <Grid>
        <MediaElement x:Name="Player" Stretch="Uniform" LoadedBehavior="Manual" UnloadedBehavior="Stop"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

从后面的代码我将其设置Source为任何https Uri:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var source = new Uri("https://stream_which_can_be_opened_with_windows_media_player.com", UriKind.Absolute);
        Player.Source = source;
        Player.Play();
    }
}
Run Code Online (Sandbox Code Playgroud)

Play()方法被称为NullReferenceException被抛出,而不是播放媒体内容.MediaElement初始化,NullReferenceExceptionPlay()方法抛出,见下文.

可以在Windows Media Player(文件 - >打开URL)中打开视频的相同Uri.

问题似乎是在MediaPlayerState.OpenMedia方法(MediaElement内部使用的对象)中,该方法试图检查从中检索的appDeploymentUri SecurityHelper.ExtractUriForClickOnceDeployedApp是否具有方案HTTPS.该应用程序未部署ClickOnce(它有一个独立的安装程序),appDeploymentUri为null,因此NullReferenceException.

这是来自PresentationFramework.dll,System.Windows.Media.MediaPlayerState.OpenMedia

    if (SecurityHelper.AreStringTypesEqual(uriToOpen.Scheme, Uri.UriSchemeHttps))
    {
        // target is HTTPS. …
Run Code Online (Sandbox Code Playgroud)

c# wpf mediaelement nullreferenceexception

6
推荐指数
1
解决办法
4606
查看次数

标签 统计

c# ×1

mediaelement ×1

nullreferenceexception ×1

wpf ×1