Using MediaElement to play video from Stream

夙愿已清 提交于 2019-11-26 18:52:26

IF you can make the WCF deliver the Media Object via a http-URL (GET) then you can just assign that URL to the MediaElement.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 the SetSource 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

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

public CustomFileStream(byte[] data)
{
    _Stream = new MemoryStream(data);
    _Length = _Stream.Length;
    _data = data;
    _Offset = 0;
}

Create a wpf application and add a media element and a button and copy the below code

public MainWindow()
{
    BoxedAppSDK.NativeMethods.BoxedAppSDK_Init();

    InitializeComponent();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    var MyFileStream = new CustomFileStream(File.ReadAllBytes(@"wildlife.wmv"));

    IntPtr ptr = BoxedAppSDK.NativeMethods.BoxedAppSDK_CreateVirtualFileBasedOnIStream(
            @"1.wmv",
            BoxedAppSDK.NativeMethods.EFileAccess.GenericWrite,
            BoxedAppSDK.NativeMethods.EFileShare.Read,
            IntPtr.Zero,
            BoxedAppSDK.NativeMethods.ECreationDisposition.New,
            BoxedAppSDK.NativeMethods.EFileAttributes.Normal,
            IntPtr.Zero,
            MyFileStream);

    using (new SafeFileHandle(ptr, true))
    {
        mediaElement1.Source = new Uri(Path.GetFullPath("1.wmv"));
        mediaElement1.LoadedBehavior = MediaState.Manual;
        mediaElement1.Play();
    }
}

- for boxed app please follow the samples and that's it.. you're in a happy world...

It's the same thing for QT Player as well.

Based on the response I'll add a complete example if the information provided isn't enough.

Happy coding....

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!)

stuartmclark

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!