我的应用程序中有一个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++;
}
}
}