iOS 視頻播放的簡單使用

來源:互聯網
上載者:User

標籤:

最近工作中有用到視頻播放的內容,分享一些簡單的用法給大家(由於網速問題,本例中使用的是本地的資源進行播放,要播放網路上的修改一些URL即可)

1.iOS9之前的視頻播放

  首先需要匯入MediaPlayer架構. 在iOS9之前視頻播放有兩種方式.

  1. 一種是帶有View的MPMoviePlayerViewController;
 // 1. 帶有View的控制器 NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil]; //  建立視頻播放器 MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; //  展示 [self presentMoviePlayerViewControllerAnimated:mpvc];

    2. 一種是不帶View的MPMoviePlayerController

 //  1. 不帶View的(這裡不帶view是指控制器的名字中沒有帶view,只是做個區分,不過它需要設定自身的view) NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil]; MPMoviePlayerController *mpc = [[MPMoviePlayerController alloc] initWithContentURL:url];//這裡大小是隨意設的,要看工作需要 mpc.view.frame = CGRectMake(40, 50, 200, 200); //  設定控制工具列的樣式 mpc.controlStyle = MPMovieControlStyleEmbedded; //  把播放視圖添加到控制器的view上 [self.view addSubview:mpc.view]; //  開始播放 [mpc play]; //  強引用 self.mpc = mpc;

3.通過監聽播放完的通知來實現自動播放下一個視頻

//  註冊通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
  /// 當播放結束了調用該方法  - (void) moviePlayerPlaybackDidFinishNotification:(NSNotification *) notification {      NSLog(@"%@",notification.userInfo);      MPMovieFinishReason reason  = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];  //  如果是正常結束的播放下一曲      if (reason == MPMovieFinishReasonPlaybackEnded) {          NSURL *url = [[NSBundle mainBundle] URLForResource:@"Cupid_高清.mp4" withExtension:nil];          self.mpc.contentURL = url;          [self.mpc play];      }  }

2.iOS9之後的視頻播放

iOS9 新增AVKit架構,新增類AVPlayerViewController用於視頻播放.注意:必須匯入兩個架構AVKit和AVFoundation 因為AVPlayerViewController本身不具備視頻播放的能力,必須給他一個AVPlayer.

代碼實現:

    //播放視頻    //注意點: 1. 必須給他一個播放AVPlayer,而AVPlayer在AVFoundation中,所以需要匯入AVFondation架構     //       2. 畫中畫在iPadAir2和iPadPro才能使用.     - (IBAction)play:(id)sender {     //  建立視頻播放器         AVPlayerViewController *playerVc = [[AVPlayerViewController alloc] init];     //  需要建立一個播放對象賦值給這個控制器         NSURL *URL = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];     //  設定播放器         playerVc.player = [AVPlayer playerWithURL:URL];         [self presentViewController:playerVc animated:YES completion:^{     //  開始播放             [playerVc.player play];         }];     //  設定代理         playerVc.delegate = self;     }

補充:畫中畫的一些常用代理方法

 - (void) playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController{     NSLog(@"即將開始畫中畫"); } - (void) playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController{     NSLog(@"已經開始畫中畫"); } - (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController{     NSLog(@"即將停止畫中畫"); } - (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {     NSLog(@"畫中畫已經停止"); } - (void) playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {     NSLog(@"開啟畫中畫失敗:%@",error); } /// 當播放器是modal出來的時候,當畫中畫的時候,是否要關閉彈出的播放控制器;預設YES - (BOOL) playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController{     NSLog(@"%s",__FUNCTION__);     return NO; }

 

iOS 視頻播放的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.