Range Request in ASP.NET MVC - Failed to play with Google browser and Opera

293 Views Asked by At

I'm using this library here and I'm using this plugin here to play the video.

Follow the code:

Controller:

[HttpGet]
public ActionResult StreamUploadedVideo()
{            
    byte[] test = null;

    using (var ctx = new Entities())
    {
        var result = ctx.Table.Where(x => x.Field == 4).FirstOrDefault();

        test = result.Movie;

        return new RangeFileContentResult(test, "video/mp4", "Name.mp4", DateTime.Now);
    }
}

View:

<video id="my-video" class="video-js" controls preload="auto" width="640" height="264" poster="MY_VIDEO_POSTER.jpg" data-setup="{}">
    <source src="@Url.Action("StreamUploadedVideo","Controller")" type='video/mp4'>
    <p class="vjs-no-js">
        To view this video please enable JavaScript, and consider upgrading to a web browser that
        <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
    </p>
</video>

Problem: When I change the video time (example: change the time from 1:00 to 10:00 minutes), I face this problem below:

Google Chrome: A network error caused the media download to fail part-way.

Opera: The media playback was aborted due to corruption problem or because the used features your browser did not support.

Image:

inserir a descrição da imagem aqui

The rest of the browsers are fine. Google and Opera is with the latest updated version of today's date: 07/04/2017

  • Micrososft Edge - Ok

  • Firefox - Ok

  • Internet Explorer - Ok

  • Opera - Error

  • Google - Error

Any Solution ?

1

There are 1 best solutions below

4
tpeczek On BEST ANSWER

There is an issue with your code as you are using DateTime.Now for modificationDate which is used for generating ETag and Last-Modified headers. As chromium (engine behind Chrome and opera) range requests can be conditional (which means they can contain If-Match/If-None-Match/If-Modified-Since/If-Unmodified-Since) it results in 412 Precondition Failed instead of 200 OK or 206 Partial Content. If the underlying content doesn't change you should be using same date, something like this.

[HttpGet]
public ActionResult StreamUploadedVideo()
{
    byte[] test = null;
    DateTime lastModificationDate = DateTime.MinValue;

    using (var ctx = new Entities())
    {
        var result = ctx.Table.Where(x => x.Field == 4).FirstOrDefault();

        test = result.Movie;
        lastModificationDate = result.LastModificationDate;
    }

    return new RangeFileContentResult(test, "video/mp4", "Name.mp4", lastModificationDate);
}