*說(shuō)明:利用AVPlayer可以制作視頻播放器、音頻播放器;使用AVPlayer播放過(guò)的格式有 .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)進(jìn)度, 參數(shù)second秒
- CMTime changedTime = CMTimeMakeWithSeconds(second, 1);
- [_player seekToTime:changedTime completionHandler:^(BOOL finished) {
- }];
4、改變播放速率 0.5為正常速度的一半 默認(rèn)rate是1
5、暫停/繼續(xù)
- [_player pause];
- [_player play];
6、播放時(shí)間(單位:秒)
- CGFloat currentPlayTime =_player.currentItem.currentTime.value/_player.currentItem.currentTime.timescale;
7、緩沖時(shí)間
- 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;//計(jì)算緩沖總進(jìn)度
在文檔使用的過(guò)程中是否遇到以下問(wèn)題:
更多建議: