AVPlayerItem:多个轨道的KVO

时间:2013-05-24 14:40:04

标签: ios objective-c cocoa-touch

我一直在尝试在初始化时为KVO注册我的AVQueuePlayer项目。下面,itemArray中的所有项都已使用网址正确创建。在代码之后,queueList被添加到AVQueuePlayer。播放器播放正常,每个项目按顺序播放,但只有第一个项目的观察者才会触发响应。但是,AVPlayerItemDidPlayToEnd适用于所有项目。

我对KVO很新。任何帮助,将不胜感激。

        for(AVPlayerItem *i in itemArray)

    {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextSong)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification object:i];
        [i addObserver:self forKeyPath:@"status" options:0 context:nil];
        [i addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];
        [i addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:0 context:nil];

        [queueList addObject:item];

    }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    AVPlayerItem *thisItem = (AVPlayerItem *)object;

    if ([keyPath isEqualToString:@"rate"]) {
        if ([self.player rate]!=0) {
            [self setPlayButtonAsPlaying:YES];
            NSLog(@"PLAYING");
        }
        else {
            [self setPlayButtonAsPlaying:NO];
            NSLog(@"PAUSED");
                    }
    }
    else if ([keyPath isEqualToString:@"status"])
    {
        if(thisItem.status==AVPlayerItemStatusFailed)
        {
            NSLog(@"failed");
            [self setPlayButtonAsPlaying:NO];
            [player pause];
        }
    }
    else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
    {
        if(thisItem.playbackBufferEmpty)
        {
            [player pause];
            [self setPlayButtonAsPlaying:NO];
        }
    }
    else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
    {
        if(!thisItem.playbackLikelyToKeepUp)
        {
            [player pause];
            [self setPlayButtonAsPlaying:NO];
        }
        else
        {
            [player play];
            [self setPlayButtonAsPlaying:YES];
        }
    }
}

编辑:我还有一个触发此命令的按钮,我用它来测试这些属性的状态。

NSLog(player.currentItem.playbackBufferEmpty ? @"Yes" : @"No");
NSLog(player.currentItem.playbackLikelyToKeepUp ? @"Yes" : @"No");

具体来说,playbackLikelyToKeepUp为false(我通过断开wifi来手动导致)。有趣的是,playBackBufferEmpty也是假的。

0 个答案:

没有答案