From a WCF Service I got a videostream. Now I want to show this videostream in a MediaElement
inside of a VisualBrush
.
I tried giving the URI directly, this doesn't work. So I wrote a simple Converter
that saves the video to HDD and returns the URI to saved file. Here is my code.
The Converter
:
[ValueConversion(typeof(Stream), typeof(Uri))]
public class MediaElementStreamConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (!(value is Stream)) {
return new Uri("");
}
string path = Path.GetTempFileName();
using (var fs = new FileStream(path, FileMode.OpenOrCreate)) {
((Stream)value).CopyTo(fs);
}
return path;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return "";
}
}
And here is the code where I use the Converter
:
<Grid Opacity="0.4" VerticalAlignment="Top">
<Grid.Background>
<VisualBrush>
<VisualBrush.Visual>
<MediaElement IsMuted="True" ScrubbingEnabled="True" LoadedBehavior="Manual" Source="{Binding stream, Converter={StaticResource MediaElementStreamConverter}}" Loaded="MediaElement_VideoDataTemplate_Loaded" />
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>
</Grid>
And here is the MediaElement_VideoDataTemplate_Loaded
method:
private void MediaElement_VideoDataTemplate_Loaded(object sender, RoutedEventArgs e) {
var mediaElement = (MediaElement)sender;
mediaElement.Play();
mediaElement.Pause();
}
The problem is, the video don't starts.