【问题标题】:-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' while using insertTimeRange in AVMutableComposition-[__NSArrayM objectAtIndex:]:在 AVMutableComposition 中使用 insertTimeRange 时,索引 0 超出空数组的范围
【发布时间】:2016-11-23 11:51:48
【问题描述】:

以下是我的代码

firstAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_strThumbForegroundVideo] options:nil];

AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

在日志中:

_strThumbForegroundVideo 的值为:

/private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov

firstAsset 的值为:

AVURLAsset: 0x1702362c0, URL = file:///private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov

firstTrack 的值为:

AVMutableCompositionTrack: 0x174033b20 trackID = 1, mediaType = vide, editCount = 0

在执行“insertTimeRange”时出现此错误,

-[__NSArrayM objectAtIndex:]; // index 0 beyond bounds for empty array' while using insertTimeRange in AVMutableComposition

我正在尝试使用 this link 上的教程合并 2 个视频。唯一的区别是,在本教程中,他们从照片中导入了视频,我一直在将最近录制的视频路径转换为 ​​AVAsset,然后完成相同的功能。我录制的视频可以在图库中找到,路径与我相同。

在这里,无论如何我都需要视频资产,因为它不是可选的。因此,不能使用类似的东西

NSArray *dataSourceArray = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeVideo]];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                                                                   ofTrack:([dataSourceArray count]>0)?[dataSourceArray objectAtIndex:0]:nil
                                                                    atTime:kCMTimeZero
                                                                     error:nil]; 

在 Xcode 8.1 中实现

【问题讨论】:

  • 在 inserttimerange 之前你应该检查 firstTrack 是 nil 还是什么?
  • 您不能在arraydictionary 中添加nil
  • 这可能是您的Asset 的问题,它可能不包含视频类型媒体AVMediaTypeVideo

标签: ios objective-c video avmutablecomposition avurlasset


【解决方案1】:

使用下面的代码,这可能对你有用

NSURL *videoURL = [NSURL fileURLWithPath:path]  // Don't use URLWithString if any one using .

AVAsset *firstAsset=[AVAsset assetWithURL:videoURL]; // get your asset

AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                        ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

请在您insertTrack 之前检查您的资产是否包含该类型的媒体轨道,这里我假设我的资产包含至少一个视频类型的媒体轨道AVMediaTypeVideo

如果您不确定,请检查包含您的资产的轨道数。 使用下面的代码

NSArray *videoTracks = [NSArray arrayWithArray: [secondAsset tracksWithMediaType:AVMediaTypeVideo]];
        NSLog(@"videoTracks count => %ld",[videoTracks count]);

        [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                            ofTrack:([videoTracks count]>0)?[videoTracks objectAtIndex:0]:nil
                             atTime:kCMTimeZero
                              error:nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多