1

我正在尝试将多个音频文件与多个剪辑合并。我已成功添加多个视频,但我无法添加多个音频文件。我尝试了以下方式并收到错误“__NSArrayM insertObject:atIndex:]: object cannot be 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];
4

1 回答 1

1

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

于 2013-01-23T19:22:58.877 回答