Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a video from URL with no file extension in iOS?

I need to play a video at this url: http://ep-lin-upload.s3.amazonaws.com/vupl_lin/F64/CDF/F64CDF3B0348471E95C244DE2DDDD3F3

The mime type of the video is: video/mp4

NSURL *adURL = [NSURL URLWithString:@"http://ep-lin-upload.s3.amazonaws.com/vupl_lin/F64/CDF/F64CDF3B0348471E95C244DE2DDDD3F3"];

AVPlayer *player = [AVPlayer playerWithURL:adURL];

This code works for urls with file extension. However, AVPlayer does not recognise the mime type if there is no file extension I think. How can I get around this?

like image 382
UtkuS Avatar asked Oct 05 '15 17:10

UtkuS


People also ask

How do I play videos on iOS?

Play a videoAs you browse photos and videos in the Photos app, tap a video to play it on your iPhone. While it plays, you can do any of the following: Tap the player controls below the video to pause, unmute, favorite, share, delete, or see video information; tap the screen to hide the player controls.


1 Answers

Note: I think that Tom's comment is correct and that the player would almost certainly handle the file correctly if the server included the correct MIME type in the response.

Having said that, the behaviour in browser is interesting even though your use case is iOS client based as you point out:

The following fiddle works fine on Chrome on a Mac and on IE on Windows but not on Safari:

<video width="320" height="240" controls>
    <source src="http://ep-lin-upload.s3.amazonaws.com/vupl_lin/F64/CDF/F64CDF3B0348471E95C244DE2DDDD3F3" type="video/mp4">
</video>

Testing on a different server, i.e not using Amazon S3, and with different mp4 videos (e.g. BigBuckBunny) produces the same result: Chrome and IE are happy even if the extension is not there but Safari seems to require it or it will not play the video.

Update (or more accurately, updated update): looking at the request and response in both Safari and Chrome it seems that Safari is also not sending any headers in the request message. After some searching this appears to be an issue that has been seen before and S3 will reject requests without referer headers. You can update the S3 policy to allow a special referer header of 'empty' - see info below.

However, given that the same behaviour exists on a different server, it seems more likely that Chrome and IE assume that the 'type=video/mp4' info in the HTML is correct and hence interpret the content this way, while Safari looks at the content type in the response to make its decision (i.e. Chrome plays the returned file as video even though the response header says 'Content-Type:application/octet-stream' rather than 'video/mp4').

For more information on the S3 header policies mentioned above, take a look at the section "Restricting Access to a Specific HTTP Referrer" in this S3 policy page for more information on policies and the referrer header:

  • http://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html#example-bucket-policies-use-case-4

In particular the following note:

Suppose you have a website with domain name (www.example.com or example.com) with links to photos and videos stored in your S3 bucket, examplebucket. By default, all the S3 resources are private, so only the AWS account that created the resources can access them. To allow read access to these objects from your website, you can add a bucket policy that allows s3:GetObject permission with a condition, using the aws:referer key, that the get request must originate from specific webpages.

like image 107
Mick Avatar answered Oct 14 '22 08:10

Mick