addObserver

来源:互联网 发布:淘宝吊带碎花长裙 编辑:程序博客网 时间:2024/06/16 11:21
 [self.myPlayerView.player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    

}

#pragma mark- KVO回调方法

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

    

    //判断被观察的对象是否为对应的媒体对象,并且观察的属性为status字段

    if (object == self.myPlayerView.player.currentItem && [keyPath isEqualToString:@"status"])

    {

        

        /*

         AVPlayerStatusUnknown,

         AVPlayerStatusReadyToPlay,

         AVPlayerStatusFailed

         */

        //判断状态是否转变为可播放状态

        if (self.myPlayerView.player.currentItem.status == AVPlayerStatusReadyToPlay)

        {

            

            /*

             //CMTime专门用于表示电影时间

             //CMTimeMake(已经播放到第几帧, 1秒钟内播放的总帧);

             CMTimeMake(50, 2);

             */

            

            //总秒数

            NSInteger totalSeconds = self.myPlayerView.player.currentItem.duration.value / self.myPlayerView.player.currentItem.duration.timescale;

            //设置进度条的最大值

            self.progressSlider.maximumValue = totalSeconds;

            //设置显示总时间大小的Label内容

            self.totalTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",totalSeconds/3600,totalSeconds%3600/60,totalSeconds%60];

            

        }

        

        //监测进度

        /*

         参数1:监测的间隔时间

         参数2:使用的队列(GCD内容,暂时置为nil

         参数3:回调block

         */

        [self.myPlayerView.player addPeriodicTimeObserverForInterval:CMTimeMake(11queue:nil usingBlock:^(CMTime time)

        {

            //CMTime time :目前播放的进度为time

            //设置进度条进度

            self.progressSlider.value = time.value / time.timescale;

            //设置显示进度秒数的label内容

            self.playTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(NSInteger)self.progressSlider.value/3600,(NSInteger)self.progressSlider.value%3600/60,(NSInteger)self.progressSlider.value%60];

        

        }];

        

        

    }

    

}



- (IBAction)progressSliderAction:(UISlider *)sender {

    

    //拖拽进度条改变播放进度

    [self.myPlayerView.player.currentItem seekToTime:CMTimeMake(sender.value1)];

    


0 0
原创粉丝点击