WPF:播放在线视频的 MediaElement

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17628445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:12:32  来源:igfitidea点击:

WPF: MediaElement to play online videos

c#.netwpfmedia-player

提问by Ramesh

I am creating a WPF media player using MediaElement class in WPF. Here i can play the local videos (Available in my PC) but i cannot play an online video. Can anybody knows what i have to do for this?

我正在使用 WPF 中的 MediaElement 类创建 WPF 媒体播放器。在这里我可以播放本地视频(在我的 PC 中可用),但我无法播放在线视频。有人知道我必须为此做什么吗?

Please help me anybody.

请帮助我任何人。

Thanks,

谢谢,

回答by Mark Carpenter

It looks like you can totally do that. However, you may running into issues with the type of video you're trying to play (file type), an authentication issue with the site you're trying to stream videos from (i.e. you need to be logged in), or the URL is malformed/incorrect. One way to test that is to hook up the MediaFailedevent handler and pop up a message whenever that's raised so you can get some insight into what's going on.

看起来你完全可以做到。但是,您可能会遇到您尝试播放的视频类型(文件类型)、您尝试从中流式传输视频的站点的身份验证问题(即您需要登录)或 URL格式错误/不正确。测试它的一种方法是连接MediaFailed事件处理程序并在引发时弹出一条消息,以便您可以深入了解正在发生的事情。

I was able to wire this up in a few minutes. The H.264 videos worked fine for me.

我能在几分钟内把它连接起来。H.264 视频对我来说效果很好。

public MainWindow()
{
    InitializeComponent();

    MyMediaElement.MediaFailed += MyMediaElement_MediaFailed;
    MyMediaElement.LoadedBehavior = MediaState.Play;
    MyMediaElement.Source = 
        new Uri(@"http://somesite/somevideo.mp4", UriKind.Absolute);
}

void MyMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
    MessageBox.Show(e.ErrorException.Message);
}

回答by somu

As WPF mediaelement internally uses windows media player. If you alter the buffer settings of media player from default buffer setting to custom. Open windows media player ? Tools ? Options ? Performance. As highlighted in the image below.

由于 WPF mediaelement 内部使用 windows 媒体播放器。如果您将媒体播放器的缓冲区设置从默认缓冲区设置更改为自定义。打开 windows 媒体播放器 ? 工具 ?选项 ?表现。如下图突出显示。

When you choose “Buffer” option and set “Seconds of content” to 2. The following registry values will be added under media player. HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Preferences UseDefaultBufferTime=0 CustomBufferTime=2000

当您选择“Buffer”选项并将“Seconds of content”设置为 2 时,将在媒体播放器下添加以下注册表值。HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Preferences UseDefaultBufferTime=0 CustomBufferTime=2000

You can use dotnet registry class to make changes. Refer this link: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1b4b8fb9-ff8f-4861-8e99-4a7a4fc75596/setting-windows-media-player-properties-in-wpf?forum=wpf#ac879a7f-37bc-4ccc-854d-ab6e047086e5

您可以使用 dotnet 注册表类进行更改。请参阅此链接:https: //social.msdn.microsoft.com/Forums/vstudio/en-US/1b4b8fb9-ff8f-4861-8e99-4a7a4fc75596/setting-windows-media-player-properties-in-wpf?forum= wpf#ac879a7f-37bc-4ccc-854d-ab6e047086e5