*說明:利用AVPlayer可以制作視頻播放器、音頻播放器;使用AVPlayer播放過的格式有 .mp4 .m3u8 .mp3
1、創(chuàng)建播放器:
- _player = [[AVPlayer alloc] initWithPlayerItem:[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@""]]];
2、切換播放地址
- AVPlayerItem *item = [AVPlayerItem playerItemWithURL:mediaUrl];
- [_player replaceCurrentItemWithPlayerItem:item];
3、跳轉(zhuǎn)進度, 參數(shù)second秒
- CMTime changedTime = CMTimeMakeWithSeconds(second, 1);
- [_player seekToTime:changedTime completionHandler:^(BOOL finished) {
- }];
4、改變播放速率 0.5為正常速度的一半 默認rate是1
5、暫停/繼續(xù)
- [_player pause];
- [_player play];
6、播放時間(單位:秒)
- CGFloat currentPlayTime =_player.currentItem.currentTime.value/_player.currentItem.currentTime.timescale;
7、緩沖時間
- NSArray *loadedTimeRanges = [[_player currentItem] loadedTimeRanges];
- CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];//獲取緩沖區(qū)域
- float startSeconds = CMTimeGetSeconds(timeRange.start);
- float durationSeconds = CMTimeGetSeconds(timeRange.duration);
- NSTimeInterval result = startSeconds + durationSeconds;//計算緩沖總進度
更多建議: