Using MediaElement to play video from Stream

前端 未结 5 913
生来不讨喜
生来不讨喜 2020-11-27 05:34

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 me

5条回答
  •  醉话见心
    2020-11-27 05:50

    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....

提交回复
热议问题