I'm working with AVFoundation
using audio only - i.e. no video - and trying to join several AVComposition
s together, one after the other, to end up with one single AVComposition
.
Example case: just two AVComposition
s. Each of them plays fine by creating an AVPlayer thus:
_player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:comp]]
where comp
is an instance of AVMutableComposition
. (Incidentally, it's worth noting that _player
has to be an ivar otherwise ARC prematurely releases it before it plays - took a while to track that one down.)
That's good - executing
[_player play]
results in comp
being played back successfully.
However, this fails:
(self.segments
is an NSMutableArray
containing elements that are a custom subclass of 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);
}
For every element of self.segments
when this code executes, I get this error when invoking the insertTimeRange::::
method:
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)}
I can't find any information about what this error indicates - seems to be just a catch-all - and I can't see what I'm doing wrong. Any ideas?
This code error corresponds to AVErrorUnknown = -11800
so there is not much explanation about the cause of the issue. https://developer.apple.com/documentation/avfoundation/averror/averrorunknown?language=objc
Just in case it might help somebody else I'll point out here my reason.
Context
In my case, I was getting the error when trying to connect to Airplay.
Cause
The cause was that I have 2 instances of AVPlayer. One for a movie type video and another one for a preview. Then when I was trying to cast the movie... AVFoundation was getting confused and it was delivering this error.
Solution
On the moviePlayback
instance:
allowsExternalPlayback = true
usesExternalPlaybackWhileExternalScreenIsActive = true
On the default
instance:
allowsExternalPlayback = false
usesExternalPlaybackWhileExternalScreenIsActive = false
In my case, CMTimeRange
was wrong and duration == 0. This is because CMTimeMake
will convert the input to an integer and it loses precision.
To solve this problem, I used a bigger time scale.
Problem code:
CMTime startTime = CMTimeMake(timeStamp.begin, 1);
CMTime duration = CMTimeMake(timeStamp.duration, 1);
Right code:
CMTime startTime = CMTimeMake(timeStamp.begin*1000, 1000);
CMTime duration = CMTimeMake(timeStamp.duration*1000, 1000);
then it works properly.
[videoTrack insertTimeRange:CMTimeRangeMake(startTime, duration) ofTrack:videoTracks.firstObject atTime:kCMTimeZero error:&error];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With