Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play movie files with no file extension on iOS with MPMoviePlayerController or AVPlayer?

I want to play a movie in iOS 4.3 on the iPad. I've successfully used MPMoviePlayerController and AVPlayer to load files from a remote URL when the filename has a file extension. However, when I use a CDN that doesn't return the filename (just an un-guessable random name), neither MPMoviePlayerController or AVPlayer seem to be able to cope.

Is there a way to tell either player that it really is a movie of type x and it should just get on playing it?

MPMoviePlayerController will return the following error from it's changed state notification:

{
    MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1;
    error = "Error Domain=MediaPlayerErrorDomain Code=-12847 \"This movie format is not supported.\" UserInfo=0x5b60030 {NSLocalizedDescription=This movie format is not supported.}";
}

I know that file is a valid m4v file, as when I rename it all is fine.

like image 436
Michael Avatar asked Mar 31 '11 14:03

Michael


People also ask

What is the extension for iOS files?

ipa file extension is used for Apple iOS application file and . ipsw file extension is used for iPhone, iPad or iPod Touch iOS software firmware update file.

How do you show file extensions on iPhone?

You control that in Finder > Preferences > Advanced, with the “Show all filename extensions” checkbox. Individual files can override the setting, so if an extension isn't doing what you want, select the file, choose File > Get Info, and check or uncheck the Hide Extension checkbox in the Info window.


3 Answers

File at tmp

NSString* _filePath

Create symlink

NSFileManager *filemgr = [NSFileManager defaultManager];
NSString *slink = [_filePath stringByAppendingPathExtension:@"m4v"];
if (![filemgr fileExistsAtPath:slink]) {
    NSError *error = nil;
    [filemgr createSymbolicLinkAtPath:[_filePath stringByAppendingPathExtension:@"m4v"] withDestinationPath: _filePath error: &error];
    if (error) {
        ...
    }
}
...

play video by slink

like image 156
Andrii Tishchenko Avatar answered Sep 24 '22 05:09

Andrii Tishchenko


If the player can't guess the file format you need to check that the CDN sends the right mime type back. My guess is that your CDN can't guess the mimetype correctly nor can the player.

In most cases this is due to how the CDN presents the HTTP header. Check that the "Content-Type" header is set to a video format matching your content.

like image 26
Magnus Avatar answered Sep 22 '22 05:09

Magnus


WebKit handle this by a Private AVURLAsset option: AVURLAssetOutOfBandMIMETypeKey, this option is used when you specify a MIME type in the HTML's video tag,

You can use this option like:

NSString * mimeType = @"video/mp4";

// or even with codecs
mimeType = @"video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"";

// create asset
AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:url options:@{@"AVURLAssetOutOfBandMIMETypeKey": mimeType}];

// create AVPlayer with AVURLAsset
AVPlayer * player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];

Since it is a private key, you may want to obfuscate it if you plan to submit it to AppStore.

The WebKit source can be found here: https://opensource.apple.com/source/WebCore/WebCore-7604.1.38.1.6/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm.auto.html

like image 32
naituw Avatar answered Sep 23 '22 05:09

naituw