IOS開發之視頻播放


介紹兩種播放視頻的方法  第一種稍微復雜一點。

第一種方式播放視頻:

一、視頻播放基礎知識

1.介紹

 ios9.0的改變 棄用了MPMoviePlayerViewController  導入MediaPlayer框架

 現使用AVPlayerViewController  導入AVFoundation框架

 

 (1)AVPlayer 播放音視頻  視頻操作相關

     《1》play

     《2》pause

     《3》seekToTime:跳轉進度 

     《4》currentItem 當前播放的視頻元素

     《5》volume 調節音量

     《6》externalPlaybackVideoGravity 視頻顯示的播放樣式

           AVLayerVideoGravityResizeAspect 普通的

           AVLayerVideoGravityResizeAspectFill 充滿的

     《7》currentTime 獲得當前播放時間 -> CMTime

          CMTimeGetSeconds(<#CMTime time#>) 通過CMTime獲得當前播放時間(秒)

 (2)AVPlayerItem 音視頻對象  視頻內容相關

     《1》duration 總時長

     《2》status 加載的狀態

         ①AVPlayerItemStatusUnknown 未知的狀態

         ②AVPlayerItemStatusReadyToPlay 准備播放狀態

         ③AVPlayerItemStatusFailed  失敗狀態

     《3》時間控制的一個類目

         ① currentTime 獲得當前播放時間

         ② forwardPlaybackEndTime跳到結束位置

         ③ reversePlaybackEndTime 調到開始位置

         ④ seekToTime: 調到指定位置

 (3)AVPlayerLayer播放顯示視頻的圖層界面

 (4)AVPlayerViewController  視圖控制器 #import <AVKit/AVKit.h> 可以顯示視頻 並且有調節控件

 2.使用

 (1)AVPlayer直接播放

   《1》創建AVPlayerItem

       <1>創建方式  

          ①playerItemWithURL:類方法通過URl地址創建要播放的對象 可以是本地內容也可以是在線內容

          ②initWithURL:構造方法

          ③ playerItemWithAsset:通過設備相冊里面的內容 創建一個 要播放的對象

          ④playerItemWithAsset:automaticallyLoadedAssetKeys自動根據要求的Key去加載相冊里面的內容

          ⑤initWithAsset:automaticallyLoadedAssetKeys:

   《2》創建AVPlayer

       <1>創建方式

        ①playerWithURL:根據URL去創建播放器 不需使用item

        ②initWithURL:

        ③playerWithPlayerItem:需要傳入一個視頻播放內容

        ④initWithPlayerItem:

        ⑤獲得播放結束的狀態

        ⑥ seekToTime:跳轉到某一個進度

        ⑦CMTime:結構體 value(進度)timeScale(幀率)

   《3》創建AVPlayerLayer 添加到父視圖

      <1>根據播放器去創建一個可以顯示視頻的圖層playerLayerWithPlayer:->類方法

      <2>設置位置frame

      <3>把視頻圖層添加到父圖層

   《4》播放******  [播放器 play];

實例代碼:

導入框架:#import <AVFoundation/AVFoundation.h> 聲明全局變量: @interface ViewController () {     AVPlayer *player; } @end 在ViewDidLoad中 - (void)viewDidLoad {     [super viewDidLoad];     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];     button.frame = CGRectMake(100, 100, 100, 100);     [button setTitle:@"TICK" forState:UIControlStateNormal];     button.backgroundColor = [UIColor brownColor];     [button addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:button];      } 按鈕的觸發方法: - (void)doit:(UIButton *)sender{          /*      視頻播放需要 AVPlayer AVPlayerItem AVPlayerLayer            AVPlayer(視頻播放器)去播放->AVPlayerItem(視頻播放的元素)->AVPlayerLayer展示播放的視圖            */      //    1.創建要播放的元素
         NSURL *url = [[NSBundle mainBundle]URLForResource:@"IMG_9638.m4v" withExtension:nil];     AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];      //    2.創建播放器
    player = [AVPlayer playerWithPlayerItem:item];           //    3.創建視頻顯示的圖層
    AVPlayerLayer *showVodioLayer = [AVPlayerLayer playerLayerWithPlayer:player];     showVodioLayer.frame = self.view.frame;     [self.view.layer addSublayer:showVodioLayer];      //    4.播放視頻
    [player play];           //    獲得播放結束的狀態 -> 通過發送通知的形式 獲得 -> AVPlayerItemDidPlayToEndTimeNotification
         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(itemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];           //    CMTime  結構體  表示視頻播放的進度的 value(進度) timescale(幀率) //    CMTimeMake(<#int64_t value#>, <#int32_t timescale#>)      //    kCMTimeZero  表示初始進度 //     seekToTime:<#(CMTime)#>  跳轉到某一個進度           //   player.currentItem.status  只要可以得到當前視頻元素准備好的狀態  就可以得到總時長了 //    采取KVO的形式獲得 視頻總時長  通過監視status 來判斷是否准備好的狀態
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];           //    快進 //    跳到某一個進度的方法  -> seekToTime: //    得到當前的時間 + 快進的時間
               } 滑動屏幕讓視頻快進 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //    獲得到當前播放的時間(秒)
  Float64 cur = CMTimeGetSeconds(player.currentTime);     cur += 2;     [player seekToTime:CMTimeMake(cur, 1)]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{          NSLog(@"%@",change[@"new"]);     AVPlayerItemStatus status = [change[@"new"] integerValue];     switch (status) {         case 0:{             NSLog(@"未知");             break;         }         case 1:{             NSLog(@"總時長:%f",CMTimeGetSeconds(player.currentItem.duration));             break;         }         case 2:{             NSLog(@"失敗");             break;         }         default:             break;     } } - (void)itemDidPlayToEndTime:(NSNotification *)not{     NSLog(@"播放結束");          [player seekToTime:kCMTimeZero] }

第二種方式比較簡單:

使用

AVPlayerViewController  視圖控制器可以顯示視頻 並且有調節控件

注意導入框架:

#import <AVFoundation/AVFoundation.h>

#import <AVKit/AVKit.h>

- (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = [UIColor orangeColor];     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];     button.frame = CGRectMake(100, 100, 100, 100);     [button setTitle:@"TICK" forState:UIControlStateNormal];     button.backgroundColor = [UIColor brownColor];     [button addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:button]; } 按鈕的觸發事件: - (void)playMovie{     //    1.AVPlayer
    AVPlayer *player = [AVPlayer playerWithURL:[[NSBundle mainBundle] URLForResource:@"IMG_9638.m4v" withExtension:nil]];     //    2.創建視頻播放視圖的控制器
         AVPlayerViewController *playerVC = [[AVPlayerViewController alloc]init];     playerVC.player = player;      //    隱藏 控制控件 //    playerVC.showsPlaybackControls = NO; //    [self presentViewController:playerVC animated:YES completion:nil];           //    自定義位置
    playerVC.view.frame = CGRectMake(200, 300, 300, 300);     [self.view addSubview:playerVC.view];     [self addChildViewController:playerVC]; }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM