1

更新:奇怪的是,这段代码在 iPad 2 上运行良好,但在 iPad 4th Gen 上运行良好。

更新#2:如果我更改presetName:AVAssetExportPresetHighestQualitypresetName:AVAssetExportPresetPassThrough视频成功导出但我无法在设备中播放。如果我通过 xCode 的管理器将应用程序包拉到我的计算机上,我就可以播放它。同样,这个问题只发生在 iPad 4 上,而不是 iPad 2、64 位模拟器、视网膜模拟器或 1x 模拟器。

我正在使用AVExportSession. 它在模拟器和 iPad 2 上运行得非常愉快,但不是 iPad 4th Gen。导出会话给出了一个-11820错误 ( AVErrorExportFailed),但这是我可以从该过程中获得的有用信息的范围。源文件存在,其他一切都在正常工作,但不是AVExportSession.

你能帮我让它在设备上工作吗?

为该方法的冗长道歉。

-(NSURL*)bindAudioAndVideo:(NSString*)audioFileName videoFileName:(NSString*)videoFileName
{

   //documents folder
   NSArray     *paths              = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsFolder       = [[NSString alloc] initWithString:[paths objectAtIndex:0]];    //Get the docs directory

    AVMutableComposition* mixComposition = [AVMutableComposition composition];

    NSString* audio_inputFileName   = audioFileName;
    NSString* audio_inputFilePath   = [documentsFolder stringByAppendingPathComponent:audio_inputFileName];
    NSURL*    audio_inputFileUrl    = [NSURL fileURLWithPath:audio_inputFilePath];

    NSString* video_inputFileName   = videoFileName;
    NSString* video_inputFilePath   = [documentsFolder stringByAppendingPathComponent:video_inputFileName];
    NSURL*    video_inputFileUrl    = [NSURL fileURLWithPath:video_inputFilePath];

    NSString* outputFileName        = @"outputFile.mp4";
    NSString* outputFilePath        = [documentsFolder stringByAppendingPathComponent:outputFileName];
    NSURL*    outputFileUrl         = [NSURL fileURLWithPath:outputFilePath];

    //Check files actually exist before beginning (they do)

    AVMutableComposition* mixComposition = [AVMutableComposition composition];
    CMTime nextClipStartTime = kCMTimeZero;

    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
    CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];


    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
    CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];



    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    _assetExport.outputURL = outputFileUrl;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         [self addSkipBackupAttributeToItemAtURL:outputFileUrl];
         NSLog(@"Completed. Tidy time.");

         switch ([_assetExport status]) {
             case AVAssetExportSessionStatusCompleted:
                 NSLog(@"Export Completed");
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export failed: %@", [[_assetExport error] localizedDescription]);
                 NSLog (@"FAIL %@",_assetExport.error); //-11820! I AM A USELESS ERROR CODE
                 NSLog (@"supportedFileTypes: %@", _assetExport.supportedFileTypes);
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export cancelled");
                 break;
             default:
                 break;
         }


            NSTimer *refreshTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(exportCompleteRefreshView) userInfo:Nil repeats:NO];

         //Throw back to main thread unuless you want really long delays for no reason.
         [[NSRunLoop mainRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
     }
     ];



    return outputFileUrl;
}
4

1 回答 1

1

如果问题与视网膜 iPad 有关 - 它与设备分辨率有关,由于某种原因模拟器没有模拟。

由于我是在设备上创建视频,因此我在视网膜设备上制作了 2048x1536 的视频(在非视网膜设备上制作了 1024x768)。显然这是太多像素AVExportSession无法处理,或者 iPad 无法正常播放,所以它只是在播放或导出时向我抛出了各种模糊的错误消息。以点分辨率而不是像素分辨率记录似乎已经解决了这个问题。

该模拟器似乎是一条红鲱鱼,因为它拥有相对无限的资源可供使用的健康 mac,而不是 A6。

于 2013-11-04T05:30:57.487 回答