代码之家  ›  专栏  ›  技术社区  ›  YosiFZ

AVPlayer状态观察失败时不呼叫

  •  0
  • YosiFZ  · 技术社区  · 6 年前

    我的应用程序中有一个AVPlayer:

    AVAsset *newAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
    AVPlayerItem *newPlayerItem = [[AVPlayerItem alloc] initWithAsset:newAsset];
    
    audioPlayer = [[AVPlayer alloc] initWithPlayerItem:newPlayerItem];
    [audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
    audioPlayer.automaticallyWaitsToMinimizeStalling = NO;
    
    avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:audioPlayer];
    [NowPlayingManager setPlayerLayer:avPlayerLayer];
    

    有时视频无法开始播放,所以我将状态观察添加到 AVPlayer :

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == audioPlayer && [keyPath isEqualToString:@"status"]) {
        if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
            [avPlayerLayer.player play];
        } else if (audioPlayer.status == AVPlayerStatusFailed) {
            [self playSong];
        } else {
            int i =0;
            i++;
        }
    }
    }
    

    0 回复  |  直到 6 年前