1

从 WCF 服务我得到了一个视频流。现在我想MediaElementVisualBrush.

我尝试直接给出URI,这不起作用。所以我写了一个简单Converter的将视频保存到 HDD 并将 URI 返回到保存的文件。这是我的代码。

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 "";
    }
}

这是我使用的代码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>

这是MediaElement_VideoDataTemplate_Loaded方法:

private void MediaElement_VideoDataTemplate_Loaded(object sender, RoutedEventArgs e) {
    var mediaElement = (MediaElement)sender;
    mediaElement.Play();
    mediaElement.Pause();
}

问题是,视频无法启动。

4

0 回答 0