heymega
heymega

Reputation: 9401

WebApi Serving Videos on Mobile Devices

I'm using WebApi to serve videos on a website. I've tested this on all major desktop browsers and the HTML5 Video tag plays the video as expected. However, I can't get this to work on iPhones (Mobile Safari). The Get() method is never called even after pressing the play button.

WEBAPI CODE

public HttpResponseMessage Get()
{

    var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/testbw2.mp4");

    var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

    if (Request.Headers.Range != null)
    {
        try
        {
            HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
            partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, _mediaType);
            return partialResponse;
        }
        catch (InvalidByteRangeException invalidByteRangeException)
        {
            return Request.CreateErrorResponse(invalidByteRangeException);
        }
    }
    else
    {
        HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
        fullResponse.Content = new StreamContent(stream);
        fullResponse.Content.Headers.ContentType = _mediaType;
        return fullResponse;
    }
}

HTML

<video controls>
    <source src="http://localhost/WebApplication24/api/range" type="video/mp4">
    Your browser does not support the video tag.
</video> 

If I change the video src to point directly to the file...

src="http://localhost/WebApplication24/Content/testbw2.mp4"

It works so I know this isn't an encoding issue.

Is there something I am doing wrong? I get the feeling mobile safari wont request the video src if the url doesn't end with .mp4

Upvotes: 2

Views: 676

Answers (1)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

Here are the possible reasons (some) that your file does not work and possible solutions:

  1. Safari is fully expecting an actually-named MP4. No other combinations of file and mime-type works. The other browsers opt for the WEBM file first, especially Chrome. source
  2. On iOS prior to 7.0.2, Safari does support HTML5 Video, the Quicktime Player has to be installed in order for this to work. source
  3. Also there are issues in which steaming doesn't work in iOS 7 where as local files does. source
  4. For some reason videos would not play on iOS unless controls="true" is set. source
  5. iOS doesn't support all the profiles that h.264 provides. You have to encode your h264 with a baseline profile only in order for it to be playable on iphone/ipad. Encoding with Miro Video Converter might help. source

Upvotes: 3

Related Questions