这是使用自托管媒体服务器的解决方法
开始吧
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,作为回报,它将从数据库中流回所需的视频