iOS————使用NSdate获取播放时间

本文介绍了在iOS开发中如何使用`addPeriodicTimeObserverForInterval`方法来控制获取视频播放时间的刷新率,并通过CMTime转换成NSDate格式,以便在应用中展示当前播放时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block;

这个函数来控制获取时间的刷新率

例如:

[_LGCustomMoviePlayerController.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time){

        //获取当前时间

        CMTime currentTime = _LGCustomMoviePlayerController.player.currentItem.currentTime;

        //转成秒数

        double currentPlayTime = currentTime.value/currentTime.timescale;

        //进度条的value

        _movieProgressSlider.value = currentPlayTime/totalMovieDuration;

        //把时间转化成  NSDate 格式

        NSDate *d = [NSDate dateWithTimeIntervalSince1970:currentPlayTime];

        // d 的打印形式为:1970——1——1 xx:xx:xx 我们需要的是 xx:xx:xx,然后进行显示

        NSString *currentTimeStr = [NSString stringWithFormat:@"%@",d];

        NSRange range;

        range.location = 11;

        range.length = 8;

        NSString *showtime = [currentTimeStr substringWithRange:range];

        self.currentTimeLabel.text = showtime;

    }];


其中CMTimeMake(1,1)表示的是:刷新一次/秒,即检测的频率;也相当于说视频显示的时间是以秒为单位进行递进的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值