Using MediaElement to play video from Stream
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!)
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.
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