C# MediaElement-从流中设置源
我正在开发WPF应用程序,它将一些声音文件存储为数据库中的字节数组。我想通过MediaElement控件播放这些文件。MediaElement具有源属性,但其类型为Uri。有人知道是否可以将字节数组转换为Uri吗C# MediaElement-从流中设置源,c#,wpf,C#,Wpf,我正在开发WPF应用程序,它将一些声音文件存储为数据库中的字节数组。我想通过MediaElement控件播放这些文件。MediaElement具有源属性,但其类型为Uri。有人知道是否可以将字节数组转换为Uri吗 谢谢这里有一个使用自托管媒体服务器的解决方案 让我们开始吧 MainWindow.xaml <MediaElement x:Name="media" /> MediaServer类 class MediaServer { private readonly Htt
谢谢这里有一个使用自托管媒体服务器的解决方案 让我们开始吧 MainWindow.xaml
<MediaElement x:Name="media" />
MediaServer类
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();
}
}
类媒体服务器
{
私有只读HttpListener _listener=new HttpListener();
私有只读函数响应方法;
公共媒体服务器(Func方法,字符串前缀)
{
如果(!HttpListener.IsSupported)
抛出新的NotSupportedException(
“需要Windows XP SP2、Server 2003或更高版本。”);
if(前缀==null)
抛出新的ArgumentException(“前缀”);
if(方法==null)
抛出新的ArgumentException(“方法”);
_listener.Prefixes.Add(前缀);
_responderMethod=方法;
_listener.Start();
}
公开募捐
{
ThreadPool.QueueUserWorkItem((o)=>
{
尝试
{
while(_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c)=>
{
var ctx=c作为HttpListenerContext;
尝试
{
字节[]buf=_responderMethod(ctx.Request);
ctx.Response.ContentLength64=基本长度;
ctx.Response.ContentType=“应用程序/八位字节流”;
ctx.Response.OutputStream.Write(buf,0,buf.Length);
}
捕获{}
最后
{
ctx.Response.OutputStream.Close();
}
},_listener.GetContext());
}
}
捕获{}
});
}
公共停车场()
{
_listener.Stop();
_listener.Close();
}
}
试试看,我能成功地播放视频,我也希望你也能这样
对于MediaServer,我已经做了一些修改
以上内容可以使用反应式扩展缩短。如果这个对你有用的话,我也会试试
此外,我们还可以使媒体服务器通用,以便在url中传递视频的id,作为回报,它将从DB返回所需的视频,搜索“wpf MediaElement播放字节数组”。举个例子,这是一个类似的问题。不知道你是否在我下面的回答中尝试过这种方法。
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();
}
}