最近工作中有用到視頻播放的內容,分享一些簡單的用法給大家(由於網速問題,本例中使用的是本地的資源進行播放,要播放網絡上的修改一些URL即可)
1.iOS9之前的視頻播放
首先需要導入MediaPlayer框架. 在iOS9之前視頻播放有兩種方式.
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;
}