話不多說 先上圖
根本思緒 1.把 AVPlayerLayer加載到 UIView 上 UIView 加載到 ScrollView上,講player加載到一個可變數組裡 2.滑動 ScrollView的時分切換 view 的頁面 切換 view 的頁面,先把上一個頁面的播放器暫停並且把它重置到初始形態 將以後也頁面的播放器從數組外面取出加載播放 1.預備步驟 (1)幾個本地視頻資源 這裡示例講到的是.mov 格式的 (2)playVideoArray = [[NSMutableArray alloc]init]; 2.代碼進程//預備代碼 用 for 循環給 scrolleview 和數組添加內容
//示例只要兩個資源就運用的2個了
for (count = 0 ; count < 2; count++) {
NSString *urlStr = [MSPath fullPathFromAssetsInMainBundle:[NSString stringWithFormat:***這裡寫上你的資源途徑***@"guideMov/guideMov%d.mov",count+1]];
NSURL *videoURL = [NSURL fileURLWithPath:urlStr];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
//運用playerItem獲取視頻的信息,以後播放時間,總時間等
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:movieURL];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
//創立AVPlayer 播放器
AVPlayer * player = [AVPlayer playerWithPlayerItem:playerItem];
//創立一個AVPlayerLayer 用於加在 UIView 上
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeaspect;
UIView *playView = [[UIView alloc]initWithFrame:(CGRect){pw * count,0,pw,ph}];
//將playerLayer參加 UIView 的 layer 層
[playView.layer addSublayer:playerLayer];
[mScrollView addSubview:playView];
//把player 加載到 playVideoArray 數組裡
[playVideoArray addObject:player];
if (count == 0) {
currentMovieIndex = count;
[player play];
}
}
以上的代碼是預備任務
上面是 scollerview 的代理辦法
pragma mark UIScrollView delegate- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//更新UIPageControl的以後頁
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.frame;
NSInteger pageChange = offset.x/bounds.size.width;
if (pageChange == currentMovieIndex) {
return;
}
if (self.guideArrow) {
[self.guideArrow removeFromSuperview];
}
//拿出上一個的播放器
AVPlayer *prePlayer = [playVideoArray objectAtIndex:currentMovieIndex];
//把它暫停
[prePlayer pause];
//把它重置
[prePlayer replaceCurrentItemWithPlayerItem://(這個是取出播放器)[self getPlayerItem]];
currentMovieIndex = pageChange;
//拿出以後的播放器
AVPlayer *curPlayer = [playVideoArray objectAtIndex:currentMovieIndex];
//停止播放
[curPlayer play];
[mPageControl setCurrentPage:pageChange];
}
- (AVPlayerItem *)getPlayerItem{
NSString *urlStr = [MSPath fullPathFromAssetsInMainBundle:[NSString stringWithFormat:@"guideMov/guideMov%ld.mov",currentMovieIndex+1]];
NSURL *videoURL = [NSURL fileURLWithPath:urlStr];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
return playerItem;
}
//以上就是完成視頻引導頁的播放了 同理也可以用來循環切換播放本地視頻
【iOS 學習筆記(一) 視頻引導頁的制造】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!