视频播放 iOS8.0 与iOS 9.0
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <AVKit/AVKit.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController () @property(nonatomic ,strong)MPMoviePlayerController * PlayerController; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // [self test1]; } -(void)test { //iOS 9.0 视频播放 //创建视频播放控制器 AVPlayerViewController * playVc = [[AVPlayerViewController alloc]init]; NSString * path = [[NSBundle mainBundle]pathForResource:@"Alizee_La_Isla_Bonita副本.mp4" ofType:nil]; //设置属性 playVc.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]]; // [self presentViewController:playVc animated:YES completion:nil]; // //设置播放器控制器的View 的大小 playVc.view.frame = CGRectMake(0, 0, 300, 300); //播放 [playVc.player play]; //将播放器的view添加到View上 [self.view addSubview:playVc.view]; } -(void)test1 { //iOS 8.0 有界面的 //获取资源路径 NSURL * url = [[NSBundle mainBundle]URLForResource:@"Alizee_La_Isla_Bonita副本.mp4" withExtension:nil]; NSLog(@"%@",url); //创建播放器控制器 MPMoviePlayerViewController * playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url]; //播放 [self presentViewController:playerController animated:YES completion:nil]; } -(void)test2 { // iOS 8.0 无界面 //获取资源路径 NSURL * url = [[NSBundle mainBundle]URLForResource:@"Alizee_La_Isla_Bonita副本.mp4" withExtension:nil]; //创建视频播放器 MPMoviePlayerController * PlayerController = [[MPMoviePlayerController alloc]initWithContentURL:url]; //一定要强引用,否则就无法播放视频 self.PlayerController = PlayerController; //准备播放 [PlayerController prepareToPlay]; //设置样式 PlayerController.controlStyle = MPMovieControlStyleFullscreen; PlayerController.view.frame = CGRectMake(0, 0, 300, 300); // PlayerController.view.frame = self.view.bounds; [self.view addSubview:PlayerController.view]; [PlayerController play]; }