jf_*_*sea 5 iphone audio avfoundation ios avcomposition
我AVFoundation只使用音频-即不使用视频-并试图将几个AVCompositions接一个接一个地结尾AVComposition。
示例案例:仅两个AVComposition秒。通过创建一个AVPlayer,它们每个都可以正常运行,因此:
_player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:comp]]
Run Code Online (Sandbox Code Playgroud)
comp的实例在哪里AVMutableComposition?(顺便说一句,值得注意的是_player必须是一个ivar,否则ARC在播放之前会过早释放它-花了一段时间才能找到它。)
很好-执行
[_player play]
Run Code Online (Sandbox Code Playgroud)
导致comp成功播放。
但是,这失败了:
(self.segments是的NSMutableArray自定义子类的包含元素AVMutableComposition)
AVMutableComposition *comp = [AVMutableComposition composition];
NSError *err;
for (AVMutableComposition* c in self.segments) {
[comp insertTimeRange:CMTimeRangeMake(kCMTimeZero, segment.duration)
ofAsset:segment atTime:comp.duration error:&err];
DLog(@"Error was %@", segment, err);
}
Run Code Online (Sandbox Code Playgroud)
对于self.segments执行此代码时的每个元素,调用该insertTimeRange::::方法时都会收到此错误:
Error was Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not
be completed" UserInfo=0x14e8e7f0 {NSLocalizedDescription=The operation could not be
completed, NSUnderlyingError=0x14d7f580 "The operation couldn’t be completed. (OSStatus
error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780)}
Run Code Online (Sandbox Code Playgroud)
我找不到有关此错误表示什么的任何信息-似乎只是一个包罗万象的东西-我看不到我在做什么错。有任何想法吗?
小智 1
就我而言,CMTimeRange是错误的,持续时间== 0。这是因为CMTimeMake将输入转换为整数并且会丢失精度。
为了解决这个问题,我使用了更大的时间尺度。
问题代码:
CMTime startTime = CMTimeMake(timeStamp.begin, 1);
CMTime duration = CMTimeMake(timeStamp.duration, 1);
Run Code Online (Sandbox Code Playgroud)
正确的代码:
CMTime startTime = CMTimeMake(timeStamp.begin*1000, 1000);
CMTime duration = CMTimeMake(timeStamp.duration*1000, 1000);
Run Code Online (Sandbox Code Playgroud)
然后它就可以正常工作了。
[videoTrack insertTimeRange:CMTimeRangeMake(startTime, duration) ofTrack:videoTracks.firstObject atTime:kCMTimeZero error:&error];
Run Code Online (Sandbox Code Playgroud)