使用 MediaElement 播放 Stream 中的视频
是否可以使用 WPF MediaElement 播放来自 System.IO.Stream 对象的流视频?正在从存储媒体文件的 WCF 服务中检索 Stream 对象。
Is it possible to use the WPF MediaElement to play streaming video from a System.IO.Stream object? The Stream object is being retrieved from a WCF service that stores the media files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在任何人浪费时间亲自发现这一点之前:不可能在 WPF 应用程序中托管 Silverlight MediaElement。原因是它是PresentationFramework.dll(WPF 不可避免)和System.Windows.dll(Silverlight 版本)中出现的多种类型之一,这些类型具有相同的名称和相同的命名空间,但类型不同。
(有人应该向微软解释为什么我们有命名空间!)
Before anyone wastes hours finding this out for themselves: it is impossible to host the Silverlight MediaElement in a WPF application. The reason for this is that it is one of a number of types that appear in PresentationFramework.dll (unavoidable for WPF) and System.Windows.dll (Silverlight versions) that have the same names and the same namespaces, but are different types.
(Someone should explain why we have namespaces to microsoft!)
可能为时已晚,如果您仍在寻找答案,希望这可能会有所帮助。
是的,您可以使用 WPF 媒体元素播放内存流中的视频。
我使用了一个名为盒装应用程序的第三方组件,感谢 BoxedApp - http://www .boxedapp.com/boxedappsdk/
我必须稍微更新一下代码才能使其适用于 byte[]。将以下构造函数从 BoxedApp 复制到 CustomFileStream 类
创建一个 wpf 应用程序并添加一个媒体元素和一个按钮,然后复制盒装应用程序的以下代码,
这就是QT Player 也是如此。
It might be too late, hopefully this might help if you're still looking for an answer.
Yes you can play video from Memory stream using WPF media element.
I have used a third party component called boxed app, A million thanks to BoxedApp - http://www.boxedapp.com/boxedappsdk/
I have to update the code a tiny bit to make it work for byte[]. Copy the below constructor to CustomFileStream class from BoxedApp
Create a wpf application and add a media element and a button and copy the below code
It's the same thing for QT Player as well.
如果您可以使 WCF 通过
http-URL (GET)
传递媒体对象,那么您只需将该 URL 分配给MediaElement.Source
属性 - 请参阅http://msdn.microsoft.com/en-us/library/system。 windows.controls.mediaelement.source.aspx。对于此类 URL 不可用/不可能的情况:
目前无法分配流 - 尽管有一些技巧可以实现这一点,但对于基于 DirectShow 的示例,请参阅 http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6191ef1a-0010-4294-a5b4-451bbadca33a/和 http://jmorrill.hjtcentral.com/Home/tabid /428/EntryId/15/WPF-Hackery-Part-I.aspx 。
另一种选择是以某种方式托管
Silverlight MediaElement
并使用SetSource
方法,该方法可以获取流并播放它......请参阅http://silverlightviewport.codeplex.com/SourceControl/list/changesets 和 http://msdn.microsoft.com/en-us/library/cc190669% 28v=vs.95%29.aspxIF you can make the WCF deliver the Media Object via a
http-URL (GET)
then you can just assign that URL to theMediaElement.Source
property - see http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.source.aspx.For cases where such URL is not available/possible:
Assigning a Stream is currently not possible - although there are some hacks to make that happen, for a DirectShow-based example see http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6191ef1a-0010-4294-a5b4-451bbadca33a/ and http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/15/WPF-Hackery-Part-I.aspx .
Another option would be to somehow host the
Silverlight MediaElement
and use theSetSource
method which can take a stream and play it... see http://silverlightviewport.codeplex.com/SourceControl/list/changesets and http://msdn.microsoft.com/en-us/library/cc190669%28v=vs.95%29.aspx我知道这不是您所要求的,但您可以在 WPF 中的窗口内托管 VLC ActiveX 组件,然后使用该 VLC 控件连接到流并显示流。这就是我通过 WPF 进行流式处理的方式。
编辑: 此页面有一个如何在 WPF 中托管 ActiveX 控件的示例
I know this isn't what you asked for but you can host a VLC ActiveX component inside a window in WPF and then use that VLC control to connect to the stream and display the stream. This is how I got streaming working through WPF.
Edit: this page has an example of how to host an ActiveX control inside WPF
由于 WPF mediaelement 内部使用 Windows Media Player。如果将媒体播放器的缓冲区设置从默认缓冲区设置更改为自定义。打开 Windows Media Player 工具 选项 性能。
当您选择“Buffer”选项并将“Seconds of content”设置为2时。以下注册表值将添加到媒体播放器下。
HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Preferences
使用默认缓冲时间=0
CustomBufferTime=2000
您可以使用 dotnet 注册表类进行更改。
参考这个链接:
<一href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/1b4b8fb9-ff8f-4861-8e99-4a7a4fc7559 6/setting-windows-media-player-properties-in-wpf?forum=wpf#ac879a7f-37bc-4ccc-854d-ab6e047086e5" rel="nofollow">https://social.msdn.microsoft.com/Forums/vstudio/en-US/1b4b8fb9-ff8f-4861-8e99-4a7a4fc7 5596/setting-windows-media-player-properties-in-wpf?forum=wpf#ac879a7f-37bc-4ccc-854d-ab6e047086e5
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.
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
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