2

在初始化时,我一直在尝试为 KVO 注册我的 AVQueuePlayer 项目。下面,itemArray所有项目都已使用 URL 正确创建。在代码之后 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 是错误的(我是通过断开我的 wifi 手动导致的)。有趣的是, playBackBufferEmpty 也是错误的。

4

0 回答 0