Ras*_*ari 6 asp.net webforms video-streaming html5-video asp.net-web-api
我有一个asp.net webforms应用程序,并从数据库中检索以varbinary格式保存的视频,并将其显示为html5视频标记.
用谷歌搜索后,我找到了一种方法,我应该异步使用ASP.Net WebApi它,它工作正常
第一个问题
当视频第一次播放并且用户点击播放按钮重放视频时,The remote host closed the connection. The error code is 0x800704CD异常将在线上播放await outputStream.WriteAsync(buffer, 0, bytesRead);.
第二个问题
当用户点击搜索栏时,视频将从第一个开始播放.
注意
Internet Explorer 11播放视频没有任何问题,但Firefox和Chrome都有问题.
我怎么解决这个问题?
这是我的代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "VideoApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class VideoController : ApiController
{
public IVideoRepository videoRepository;
public HttpResponseMessage Get(long id)
{
try
{
videoRepository = new VideoRepository();
Video video = videoRepository.load(id);
if (video != null)
{
var videoStream = new VideoStream(video.fileContent);
string ext = video.extension;
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)videoStream.WriteToStream, new MediaTypeHeaderValue("video/" + ext));
response.Content.Headers.Add("Content-Disposition", "attachment;filename=" + video.fullName.Replace(" ", ""));
response.Content.Headers.Add("Content-Length", videoStream.FileLength.ToString());
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, e);
}
}
}
public class VideoStream
{
private readonly byte[] _fileContent;
private long _contentLength;
public long FileLength
{
get { return _contentLength; }
}
public VideoStream(byte[] content)
{
_contentLength = content.Length;
_fileContent = content;
}
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(_fileContent, 0, _fileContent.Length);
memoryStream.Position = 0;
using (memoryStream)
{
var length = (int)memoryStream.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = memoryStream.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
outputStream.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
在这种方式没有正常工作后,我不得不使用这种方式,但新的方式有seekbar问题,当用户点击搜索栏寻求时间它不能在Chrome和FireFox中工作.
ASP.NET 不太擅长视频流。第三方视频流解决方案是最好的选择。
有一些视频流服务器(例如Wowza),但它们需要安装并且您必须购买许可证。
云流媒体服务是另一种选择。我个人更喜欢AWS Cloudfront。他们建议在各个全球分布的内容交付区域中进行分发。它的成本非常便宜,并且您可以确定它能够承受任何流量(即使您的所有用户都会同时观看相同的视频)。
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |