您的位置:首页 > 移动开发 > IOS开发

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

2014-06-16 13:43 519 查看
通过
- (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)表示的是:刷新一次/秒,即检测的频率;也相当于说视频显示的时间是以秒为单位进行递进的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息