Question

I am using an MPMediaPickerController to allow the user to select videos and songs from the library on the device. I allow this with the: initWithMediaTypes:MPMediaTypeAny initialization for the picker. The user can then play the song or video in-app after the export takes place. Here is my movie-exporting code after stripping it to its core functionality:

- (void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)mediaItemCollection {
    AVAssetExportSession *exportSession;
    NSString *filePath;
    NSURL *fileUrl;

    for (MPMediaItem *item in mediaItemCollection.items) {
        NSURL *assetUrl = [item valueForProperty:MPMediaItemPropertyAssetURL];
        AVAsset *currAsset = [AVAsset assetWithURL:assetUrl];
        exportSession = [[AVAssetExportSession alloc] initWithAsset:[AVAsset assetWithURL:assetUrl] presetName:AVAssetExportPresetHighestQuality];
        exportSession.shouldOptimizeForNetworkUse = YES;
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;
        filePath = [title stringByAppendingString:@".mov"];
        fileUrl = [NSURL fileURLWithPath:[[NSFileManager documentDirectory] stringByAppendingPathComponent:filePath]];

        exportSession.outputURL = fileUrl;
        dispatch_group_enter(dispatchGroup);

        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            // success
        }
        dispatch_group_leave(dispatchGroup);
    }];
    }

This similar code works fine for doing audio, but for video, the video's audio does not play. Most content from iTunes is protected and non-exportable, so I wanted to test with a homemade quick video I shot with my iPhone. I shot the video, dragged it into iTunes (and I made it a "music video" so that it shows up properly and can be exported to my phone's library). Then I sync'd and sent it to my device for testing.

In the app, the video shows up fine in the Media Picker, and I can export it with no errors that I can see. However, when I play it in-app, it only plays the video and not the audio. Other videos that I import from other sources work fine for playing the video's audio, so I don't 'think' it's the player itself.

Is there something I may be missing here on why the audio would not be coming across from this kind of export from the media picker? Thanks in advance for any assistance on this issue!

Was it helpful?

Solution

Not sure if this is the ideal solution, but the only way we found around this issue was to change it to force m4v format with PresetPassthrough set. I.e:

exportSession = [[AVAssetExportSession alloc] initWithAsset:[AVAsset assetWithURL:assetUrl] presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeAppleM4V;
filePath = [title stringByAppendingString:@".m4v"];

Audio and video seems to work fine for videos imported locally this way, after making that change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top