NSURL *songUrl = [NSURLURLWithString:self.songList[self.i]];
self.playerItem = [AVPlayerItemplayerItemWithURL:songUrl];
[self.playerItemaddObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];// 监听status属性
self.song = [AVPlayerplayerWithPlayerItem:self.playerItem];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object ==self.playerItem && [keyPathisEqualToString:@"status"]) {
AVPlayerStatus status = [[changeobjectForKey:NSKeyValueChangeNewKey]integerValue];
if (status ==AVPlayerStatusReadyToPlay)
self.musicView.play.enabled = YES;
self.musicView.next.enabled = YES;
self.musicView.last.enabled = YES;
//只有在播放状态才能获取视频时间长度
AVPlayerItem *playerItem = (AVPlayerItem *)object;
NSTimeInterval duration =CMTimeGetSeconds(playerItem.asset.duration);
CMTime ctime =self.song.currentTime;
self.currentTimeSec = (int)ctime.value/ctime.timescale;
self.videoDuration = duration;
[selfonButtonClick:self.musicView.play];
}
elseif (self.song.status ==AVPlayerStatusFailed) {
// something went wrong. player.error should contain some information
NSLog(@"not fineee");
NSLog(@"%@",self.song.error);
}
elseif (self.song.status ==AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}