我正在尝试将 AVPlayerItem 的 AVURLAsset 插入到这样的AVPlayerItemStatusReadyToPlay
状态AVMutableComposition
:
composition_ = [[AVMutableComposition alloc] init];
insertionPoint_ = kCMTimeZero;
item_ = [[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]] retain];
[item_ addObserver:self forKeyPath:@"status" options:0 context:nil];
player_ = [[AVPlayer playerWithPlayerItem:item_] retain];
[player_ addObserver:self forKeyPath:@"currentItem.duration" options:0 context:nil];
/**
* append a player-item to our composition
*/
- (void)addItemToComposition:(AVPlayerItem *)item
{
NSError *error = nil;
VTRACE(@"item duration: %g", CMTimeGetSeconds(item.duration));
if (![composition_ insertTimeRange:CMTimeRangeMake(kCMTimeZero, item.duration)
ofAsset:item.asset
atTime:insertionPoint_
error:&error])
{
VTRACE(@"error: %@", error);
}
}
/**
* simplified value observer callback
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([object isKindOfClass:[AVPlayerItem class]])
{
AVPlayerItem *item = (AVPlayerItem *)object;
//playerItem status value changed?
if ([keyPath isEqualToString:@"status"])
{ //yes->
switch(item.status)
{
case AVPlayerItemStatusFailed:
VTRACE(@"player item status failed");
break;
case AVPlayerItemStatusReadyToPlay:
VTRACE(@"player item status is ready to play");
[self addItemToComposition:player_.currentItem];
break;
case AVPlayerItemStatusUnknown:
VTRACE(@"player item status is unknown");
break;
}
}
}
else if([object isKindOfClass:[AVPlayer class]])
{
if ([keyPath isEqualToString:@"currentItem.duration"])
{ //yes->
VTRACE(@"player item duration available");
}
}
}
不幸的是,我得到的只是尝试调用的这个糟糕的错误消息AVMutableComposition insertTimeRange:ofAsset:atTime:error:
2011-11-29 22:24:59.446 XXX[13626:10703] -[XXX addItemToComposition:] -- 错误:错误域=AVFoundationErrorDomain 代码=-11800“操作无法完成”UserInfo=0x7699f30 {NSLocalizedFailureReason=未知发生错误(-12780),NSUnderlyingError=0xd124dd0“操作无法完成。(OSStatus错误-12780。)”,NSLocalizedDescription=操作无法完成}
我错过了什么?
为什么无法将项目插入到合成中?
在检查AVURLAsset
时,AVPlayerItem
我还注意到轨道数组已分配但为空。这可能是原因吗?如果是这样,我如何AVURLAsset
才能正确保存有效曲目?