16

我正在尝试将 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才能正确保存有效曲目?

4

1 回答 1

14

好吧,我从另一个来源得到了答案,但我不喜欢它...

显然只能将本地项目(资产)插入到 中AVMutableComposition,远程项目(如 HTTP 视频流)将不起作用。

文档没有这样说以及我收到的错误消息毫无用处的事实似乎是 Apple 的遗漏。我确实提交了错误报告以改善这种情况。

苹果雷达错误 ID:10517711

打开雷达错误报告

于 2011-12-02T10:57:12.850 回答