Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当前播放位置不为';在html视频上不能改变_Javascript_Html_Video - Fatal编程技术网

Javascript 当前播放位置不为';在html视频上不能改变

Javascript 当前播放位置不为';在html视频上不能改变,javascript,html,video,Javascript,Html,Video,我有一个html5视频播放器驻留在mvc3 razor视图上。我的视频播放得很好,但奇怪的是,我似乎无法通过视频元素的滑块控制来更改播放位置(时间)。我用鼠标改变播放位置,但它只是从它离开的地方继续 我想我应该有一些额外的javascript来处理视频搜索,但这只是胡说八道,不是吗?我错过了什么 下面是我的html <video id="presentedFile" width="780" height="510" controls> <source src="/Fol

我有一个html5视频播放器驻留在mvc3 razor视图上。我的视频播放得很好,但奇怪的是,我似乎无法通过
视频
元素的滑块控制来更改播放位置(时间)。我用鼠标改变播放位置,但它只是从它离开的地方继续

我想我应该有一些额外的javascript来处理视频搜索,但这只是胡说八道,不是吗?我错过了什么

下面是我的html

<video id="presentedFile" width="780" height="510" controls>
    <source src="/Folder/GetVideoStream?videoId=3" type="video/mp4">
</video>
从答案开始,我设法克服了这个问题。我想是响应中缺少的http头
accept ranges
content range
导致无法播放视频

为了简化上面链接中提到的解决方案,他们使用了http处理程序来解决这个问题。但我想声明,使用实现http处理程序不是所需解决方案的一部分。解决方案是,您必须为响应添加必要的标题,如下所示:

public FileResult GetVideoStream( string videoId )
{
    /// create the stream

    /// if request contains range details
    if ( !String.IsNullOrEmpty(HttpContext.Request.ServerVariables["HTTP_RANGE"]) )
        SetHeadersForRangedRequests(stream, HttpContext);

    return File(myStream, MimeMapping.GetMimeMapping(myVideo));
}
下面的方法引用了上面的链接,我只是使用
StreamReader
周围的
删除了
,因为我需要在操作完成后让流保持打开状态

void SetHeadersForRangedRequests ( Stream stream, HttpContextBase context )
{
    long size, start, end, length, fp = 0;
    StreamReader reader = new StreamReader(stream);

    size = reader.BaseStream.Length;
    start = 0;
    end = size - 1;
    length = size;

    context.Response.AddHeader("Accept-Ranges", "0-" + size);

    if ( !String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]) )
    {
        long anotherStart = start;
        long anotherEnd = end;
        string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
        string range = arr_split[1];

        // Make sure the client hasn't sent us a multibyte range
        if ( range.IndexOf(",") > -1 )
        {
            // (?) Shoud this be issued here, or should the first
            // range be used? Or should the header be ignored and
            // we output the whole content?
            context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
            throw new HttpException(416, "Requested Range Not Satisfiable");
        }

        // If the range starts with an '-' we start from the beginning
        // If not, we forward the file pointer
        // And make sure to get the end byte if spesified
        if ( range.StartsWith("-") )
        {
            // The n-number of the last bytes is requested
            anotherStart = size - Convert.ToInt64(range.Substring(1));
        }
        else
        {
            arr_split = range.Split(new char[] { Convert.ToChar("-") });
            anotherStart = Convert.ToInt64(arr_split[0]);
            long temp = 0;
            anotherEnd = ( arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp) ) ? Convert.ToInt64(arr_split[1]) : size;
        }
        /* Check the range and make sure it's treated according to the specs.
            * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
            */
        // End bytes can not be larger than $end.
        anotherEnd = ( anotherEnd > end ) ? end : anotherEnd;
        // Validate the requested range and return an error if it's not correct.
        if ( anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size )
        {

            context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
            throw new HttpException(416, "Requested Range Not Satisfiable");
        }
        start = anotherStart;
        end = anotherEnd;

        length = end - start + 1; // Calculate new content length
        fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
        context.Response.StatusCode = 206;
    }

    // Notify the client the byte range we'll be outputting
    context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
    context.Response.AddHeader("Content-Length", length.ToString());
} 

返回的url是什么。如果它是绝对的,那么它就可以工作,否则它就不能工作。当我将url提供给浏览器时,它开始下载文件。我自己创建流,所以它不是一个我可以给出绝对路径的文件。
void SetHeadersForRangedRequests ( Stream stream, HttpContextBase context )
{
    long size, start, end, length, fp = 0;
    StreamReader reader = new StreamReader(stream);

    size = reader.BaseStream.Length;
    start = 0;
    end = size - 1;
    length = size;

    context.Response.AddHeader("Accept-Ranges", "0-" + size);

    if ( !String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]) )
    {
        long anotherStart = start;
        long anotherEnd = end;
        string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
        string range = arr_split[1];

        // Make sure the client hasn't sent us a multibyte range
        if ( range.IndexOf(",") > -1 )
        {
            // (?) Shoud this be issued here, or should the first
            // range be used? Or should the header be ignored and
            // we output the whole content?
            context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
            throw new HttpException(416, "Requested Range Not Satisfiable");
        }

        // If the range starts with an '-' we start from the beginning
        // If not, we forward the file pointer
        // And make sure to get the end byte if spesified
        if ( range.StartsWith("-") )
        {
            // The n-number of the last bytes is requested
            anotherStart = size - Convert.ToInt64(range.Substring(1));
        }
        else
        {
            arr_split = range.Split(new char[] { Convert.ToChar("-") });
            anotherStart = Convert.ToInt64(arr_split[0]);
            long temp = 0;
            anotherEnd = ( arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp) ) ? Convert.ToInt64(arr_split[1]) : size;
        }
        /* Check the range and make sure it's treated according to the specs.
            * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
            */
        // End bytes can not be larger than $end.
        anotherEnd = ( anotherEnd > end ) ? end : anotherEnd;
        // Validate the requested range and return an error if it's not correct.
        if ( anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size )
        {

            context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
            throw new HttpException(416, "Requested Range Not Satisfiable");
        }
        start = anotherStart;
        end = anotherEnd;

        length = end - start + 1; // Calculate new content length
        fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
        context.Response.StatusCode = 206;
    }

    // Notify the client the byte range we'll be outputting
    context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
    context.Response.AddHeader("Content-Length", length.ToString());
}