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

后端 未结 8 1571
长发绾君心
长发绾君心 2020-12-05 13:58

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 exte

相关标签:
8条回答
  • 2020-12-05 14:11

    If you have problems to get the ContentType of your connection you could cycle through the playable MIME types and create symbolic links to the actual file with the extension and check if they are playable. Like so:

    NSLog(@"linked path: %@",[videoURL absoluteString]);
    NSString* linkedPath;
    AVURLAsset* asset;
    NSFileManager *filemgr = [NSFileManager defaultManager];
    
    for (NSString* string in [AVURLAsset audiovisualMIMETypes]) {
    
        if ([string containsString:@"video/"]) {
    
            NSLog(@"Trying: %@",string);
    
            linkedPath = [[videoURL absoluteString] stringByAppendingPathExtension:[string stringByReplacingOccurrencesOfString:@"video/" withString:@""]];
            NSLog(@"linked path: %@",linkedPath);
    
            if (![filemgr fileExistsAtPath:linkedPath]) {
                NSError *error = nil;
                [filemgr createSymbolicLinkAtURL:[NSURL URLWithString:linkedPath] withDestinationURL:videoURL error:&error];
                if (error) {
                   NSLog(@"error %@",error.localizedDescription);
                }
            }
    
           asset = [AVURLAsset assetWithURL:[NSURL URLWithString:linkedPath]];
            if ([asset isPlayable]) {
                NSLog(@"Playable");
                break;
            }else{
                NSLog(@"Not Playable");
                asset = nil;
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 14:14

    It's sort of a hack, but what you could do is run each name through a method that checks for a period with three characters after it. If not, just append .m4v automatically. Or get the MIME type and append an extension automatically based on the returned type. If available. Look up documentation with NSString for more info. Good luck! Let me know if that helped.

    0 讨论(0)
  • 2020-12-05 14:18

    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

    0 讨论(0)
  • 2020-12-05 14:18

    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

    0 讨论(0)
  • 2020-12-05 14:19

    Dylan is correct.

    Both MPMoviePlayer and AVPlayer needs a file extension in order to play the file from URL otherwise an error message will be shown. Better to use some kind of tricks.

    0 讨论(0)
  • 2020-12-05 14:22

    iPhone support video H.264, MPEG-4 in .mp4, .m4v, .mov formats and audio files in AAC, MP3, M4a, Apple lossless and Audible. You can use NSFileManager's -contentsOfDirectoryAtPath:error: method to get an array with the contents of a directory (as strings).Then you just do strings operations .

    0 讨论(0)
提交回复
热议问题