日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AVPlayer设置从哪儿开始播放

發布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AVPlayer设置从哪儿开始播放 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

avplayer 播放視頻

首先介紹幾個方法吧和屬性吧。

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

這個方法可以用于跟新進度條。

- (void)seekToTime:(CMTime)time completionHandler:(void?(^)(BOOL?finished))completionHandler

這個是設置從哪個位置開始播放

?CMTime?changedTime =?CMTimeMakeWithSeconds(timeFloat,?1.0);

獲取?CMTime

rate 播放的狀態 ?0 表示暫停 ?1表示播放

volume 聲音

下面是主要的代碼

1 +(Class)layerClass 2 { 3 return [AVPlayerLayer class]; 4 } 5 6 -(void)setMoviePlayer:(AVPlayer *)moviePlayer 7 { 8 AVPlayerLayer *layer = (AVPlayerLayer *)[self layer]; 9 layer.videoGravity = AVLayerVideoGravityResizeAspectFill; 10 layer.player = moviePlayer; 11 } 12 13 -(AVPlayer *)moviePlayer 14 { 15 AVPlayerLayer *layer = (AVPlayerLayer *)[self layer]; 16 return layer.player; 17 }

?

?

?

1 /** 2 * 初始化視頻播放器 3 * 4 * @param movieUrl url網址 5 */ 6 -(void)setMovieUrl:(NSString *)movieUrl 7 { 8 _movieUrl = movieUrl; 9 _movieItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:movieUrl]]; 10 _moviePlayer = [AVPlayer playerWithPlayerItem:_movieItem]; 11 _moviePlayer.volume = 0.5; 12 [_voiceView setProgressView:0.5]; 13 _playerLayer.moviePlayer = _moviePlayer; 14 [self addNotifiction]; 15 [_playerLayer.moviePlayer pause]; 16 } /*** 添加監聽*/ -(void)addNotifiction {[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedNotifiction:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerLayer.moviePlayer.currentItem];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackError:) name:AVPlayerItemNewAccessLogEntryNotification object:_playerLayer.moviePlayer.currentItem];[_movieItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil]; }

?

?

1 //獲取播放的進度 2 AVPlayerItem *mobieItem = _movieItem; 3 __block LSCacheView * progress = _progressView; 4 [_moviePlayer addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { 5 float current=CMTimeGetSeconds(time); 6 float total=CMTimeGetSeconds([mobieItem duration]); 7 if (current) { 8 [progress setProgressView:(current/total)]; 9 } 10 }];

?

if ([keyPath isEqualToString:@"loadedTimeRanges"]) {NSTimeInterval timeInterval = [self availableDuration];NSTimeInterval cuttTime = CMTimeGetSeconds(_movieItem.currentTime);CMTime duration = _movieItem.duration;CGFloat totalDuration = CMTimeGetSeconds(duration);if (cuttTime < timeInterval){//有時候網絡卡會自動暫停,通過這個方式可以避免這種方式if (_playerLayer.moviePlayer.rate == 0 && _playerButton.isSelected == YES){[_playerLayer.moviePlayer play];}}else{}}

?

?以上這些我認為就是主要的代碼了。哦 ? 還有個進入全屏播放的方式,大部分的項目都用到了UINavigationController,所以我用的橫屏播放方式是模態跳轉的方式

在需要橫屏的controller添加以下代碼

1 - (BOOL)shouldAutorotate 2 { 3 return NO; 4 } 5 6 -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 7 { 8 return UIInterfaceOrientationLandscapeRight; 9 } 10 11 -(NSUInteger)supportedInterfaceOrientations 12 { 13 return UIInterfaceOrientationMaskLandscapeRight; 14 }

?

這便可以跳轉進入的時候橫屏了

總結

以上是生活随笔為你收集整理的AVPlayer设置从哪儿开始播放的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。