我正在尝试将多个音频文件与多个剪辑合并。我已经成功添加了多个视频,但是我无法添加多个音频文件。我尝试了以下方法,并收到错误消息“ __NSArrayM insertObject:atIndex:]:对象无法nil”。我也通过添加AVURLAsset进行了尝试,但收到错误“ [AVURLAsset资产]:无法识别的选择器发送到实例”。该应用在此代码的最后一行崩溃了。请找出解决方案或给出一个示例
 - (BOOL)insertTimeRanges:(NSArray *)timeRanges ofTracks:(NSArray *)tracks atTime:(CMTime)startTime error:(NSError **)error

NSMutableArray *audioUrlArray= [[NSMutableArray alloc]init];
NSMutableArray *audioTimeRangesArray = [[NSMutableArray alloc]init];
CMTime audioTime = kCMTimeZero;
for (int i = 0; i < self.selectedSongsArray.count; i++)
{
    MPMediaItem * songItem = [self.selectedSongsArray objectAtIndex:i];
    NSURL *songURL = [songItem valueForProperty: MPMediaItemPropertyAssetURL];
    AVURLAsset *audioUrl = [AVURLAsset URLAssetWithURL:songURL options:nil];
    AVAssetTrack *track = [[audioUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    AVAsset *audioAsset = [AVAsset assetWithURL:songURL];
    [audioUrlArray addObject:track];
    CMTimeRange timeRange = CMTimeRangeMake(audioTime, audioAsset.duration);
    [audioTimeRangesArray addObject:[NSValue valueWithCMTimeRange:timeRange]];
}
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

[audioTrack insertTimeRanges:audioTimeRangesArray ofTracks:audioUrlArray atTime:kCMTimeZero error:nil];

最佳答案

我有同样的问题,添加一个可变数组来保存AVURLAsset。一旦for循环结束,ARC将收集AVURLAsset,并且您正在存储的位将被破坏。

08-27 19:14