OSStatus错误 - 12780时调用insertTimeRange:ofTrack:atTime:错误:AVMutableCompositionTrack第二次

Sah*_*har 7 iphone video edit

首先,我要说我喜欢这个论坛,它帮助了我很多时间.我有一个问题,我无法在任何地方找到答案,所以这是我的第一个问题.

我的问题是:

我有一个由AVPlayerItem表示的视频,用户可以使用cutBefore按钮编辑视频开始时间,该按钮将视频剪切到滑块的左侧

负责剪切视频的方法如下:

- (void)CutBeforeAction { 

AVMutableComposition *composition = [AVMutableComposition composition];

// Get the audio and video tracks of the video
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// Calculate the new duration
CMTime currStartTime = _player.currentItem.currentTime;
CMTime endTime = _player.currentItem.duration;
CMTimeRange range = CMTimeRangeFromTimeToTime(currStartTime, endTime);

// Insert the new duration to the tracks
NSError *error = nil;
[compositionVideoTrack insertTimeRange:range 
                               ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                atTime:kCMTimeZero
                                 error:&error];

[compositionAudioTrack insertTimeRange:range 
                               ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                atTime:kCMTimeZero
                                 error:&error];
// Create a new AVPlayerItem with the new composition
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:composition];
[self setPlayerItem:item];
[_player replaceCurrentItemWithPlayerItem:item];

// change the player location to the beginning of the video
[_player seekToTime:CMTimeMakeWithSeconds(0, 1)];
[self syncTimeLabel];
[self syncScrubber];
Run Code Online (Sandbox Code Playgroud)

}

当第一次运行该- (void)cutBefore方法时,它运行正常,当我第二次运行它时(视频已被编辑一次)

[compositionVideoTrack insertTimeRange:range 
                           ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                            atTime:kCMTimeZero
                             error:&error];
Run Code Online (Sandbox Code Playgroud)

[compositionAudioTrack insertTimeRange:range 
                           ofTrack:[[_player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                            atTime:kCMTimeZero
                             error:&error];
Run Code Online (Sandbox Code Playgroud)

方法返回时出现以下错误:

该操作无法完成.(OSStatus错误-12780.)

我试图寻找错误代码的含义,但实际上什么也没找到.

谢谢您的帮助

Sah*_*har 0

我找到了解决此问题的方法,似乎使用insertTimeRange:ofTrack:atTime:error:AVMutableComposition 的轨道作为输入轨道的方法会导致错误,所以我所做的是保存初始资产(这不是 AVMutableComposition 的实例,而是 AVAsset 的实例),并使用它的轨迹作为输入insertTimeRange:ofTrack:atTime:error: