【问题标题】:MediaElement - set Source from StreamMediaElement - 从流中设置源
【发布时间】:2014-08-15 08:30:42
【问题描述】:

我正在开发 WPF 应用程序,它将一些声音文件作为字节数组存储在数据库中。我想通过 MediaElement 控件播放这些文件。 MediaElement 具有 Source 属性,但其类型是 Uri。有谁知道是否可以将字节数组转换为 Uri?

谢谢

【问题讨论】:

  • 搜索“wpf MediaElement 播放字节数组”。例如,here 是类似的问题。
  • 想知道您是否尝试过我下面回答中的方法。

标签: c# wpf


【解决方案1】:

这是使用自托管媒体服务器的解决方法

开始吧

MainWindow.xaml

<MediaElement x:Name="media" />

MainWindow.cs

public MainWindow()
{
    InitializeComponent();

    //host a media server on some port
    MediaServer ws = new MediaServer(RenderVideo, "http://localhost:8080/");
    ws.Run();

    //set the media server's url as the source of media element
    media.Source = new Uri("http://localhost:8080/");
}

private byte[] RenderVideo(HttpListenerRequest r)
{
    //get the video bytes from the server etc. and return the same
    return File.ReadAllBytes("e:\\vids\\Wildlife.wmv");
}

媒体服务器类

class MediaServer
{

    private readonly HttpListener _listener = new HttpListener();
    private readonly Func<HttpListenerRequest, byte[]> _responderMethod;

    public MediaServer(Func<HttpListenerRequest, byte[]> method, string prefix)
    {
        if (!HttpListener.IsSupported)
            throw new NotSupportedException(
                "Needs Windows XP SP2, Server 2003 or later.");

        if (prefix == null)
            throw new ArgumentException("prefix");


        if (method == null)
            throw new ArgumentException("method");

        _listener.Prefixes.Add(prefix);

        _responderMethod = method;
        _listener.Start();
    }


    public void Run()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            try
            {
                while (_listener.IsListening)
                {
                    ThreadPool.QueueUserWorkItem((c) =>
                    {
                        var ctx = c as HttpListenerContext;
                        try
                        {
                            byte[] buf = _responderMethod(ctx.Request);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.ContentType = "application/octet-stream";
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                        }
                        catch { }
                        finally
                        {
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch { }
        });
    }

    public void Stop()
    {
        _listener.Stop();
        _listener.Close();
    }
}

试一试,我可以成功播放视频了,希望你也一样

对于 MediaServer,我使用了 Simple C# Web Server 并进行了一些修改。

上面的内容可以使用响应式扩展来缩短。如果这对你有用,我会尝试相同的。

我们还可以使媒体服务器通用,以在 url 中传递视频的 id,作为回报,它将从数据库中流回所需的视频

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多