C# MediaElement查询

C# MediaElement查询,c#,wpf,mediaelement,C#,Wpf,Mediaelement,在我的应用程序中,我从数据库中以字节[]的形式获取视频文件,我的要求是使用WPF媒体元素播放该视频文件。 只是想知道什么是最好的和优雅的方式来做到这一点 您可以使用此功能使用媒体元素在wpf中播放视频 这是给最好的结果,我已经使用这个 请通过此链接了解更多关于 // ///处理媒体项目的删除事件。 /// 私有无效介质放置(对象发送器、DragEventArgs e) { string[]filename=e.Data.GetData(DataFormats.FileDrop,true) 作为

在我的应用程序中,我从数据库中以字节[]的形式获取视频文件,我的要求是使用WPF媒体元素播放该视频文件。
只是想知道什么是最好的和优雅的方式来做到这一点

您可以使用此功能使用媒体元素在wpf中播放视频

这是给最好的结果,我已经使用这个

请通过此链接了解更多关于

//
///处理媒体项目的删除事件。
/// 
私有无效介质放置(对象发送器、DragEventArgs e)
{
string[]filename=e.Data.GetData(DataFormats.FileDrop,true)
作为字符串[];
//保留添加文件的字典
foreach(文件名中的字符串f)
{
if(IsValidMediaItem(f))
添加(f.Substring(f.LastIndexOf(@“\”)+1),
新MediaItem(@f,0));
}
//现在添加到列表中
foreach(mediaItems.Values中的MediaItem mi)
lstmediatems.Items.Add(mi);
//将事件标记为已处理,
//因此不调用控件的本机Drop处理程序。
e、 已处理=正确;
}
/// 
///检查拖动的项目是否有效
/// 
///如果文件名有效,则为true
私有bool IsValidMediaItem(字符串文件名)
{
bool isValid=false;
字符串fileExtesion=filename.Substring(filename.LastIndexOf(“.”);
foreach(MediaItem.allowableMediaTypes中的字符串s)
{
如果(s.Equals)(fileExtesion,
StringComparison.CurrentCultureInogoreCase)
isValid=true;
}
返回有效;
}

我希望它能帮助你……

谢谢你的回答,但我的问题不同。我从数据库中获取字节数组,我的问题是如何处理字节[],这样我就可以播放视频而不会出现任何性能问题。一种解决方案是将字节[]转换为内存流并播放视频,但我认为这不是正确的方法,因为不建议将整个视频加载到内存流中。那么,做这件事的最佳方式是什么呢?
 /// <summary>
/// Handles Drop Event for Media Items.
/// </summary>
private void Media_Drop(object sender, DragEventArgs e)
{
    string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) 
        as string[];
    //keep a dictionary of added files
    foreach (string f in fileNames)
    {
        if (IsValidMediaItem(f))
            mediaItems.Add(f.Substring(f.LastIndexOf(@"\")+1),
    new MediaItem(@f,0));
    }

    //now add to the list
    foreach (MediaItem mi in mediaItems.Values)
        lstMediaItems.Items.Add(mi);

    // Mark the event as handled,
    // so the control's native Drop handler is not called.
    e.Handled = true;
}

/// <summary>
/// check to see if dragged items are valid
/// </summary>
/// <returns>true if filename is valid</returns>
private bool IsValidMediaItem(string filename)
{
    bool isValid = false;
    string fileExtesion = filename.Substring(filename.LastIndexOf("."));
    foreach (string s in MediaItem.allowableMediaTypes)
    {
        if (s.Equals(fileExtesion, 
    StringComparison.CurrentCultureIgnoreCase))
            isValid = true;
    }
    return isValid;
}