iOS开发--利用MPMoviePlayerController播放视频简单实现
一.包含头文件#import <MediaPlayer/MediaPlayer.h>
二.重点:给MPMoviePlayerController的view设置frame,并且将view添加到某一个view上
1 #import "ViewController.h" 2 #import <MediaPlayer/MediaPlayer.h> 3 4 @interface ViewController () 5 6 /* 播放器 */ 7 @property (nonatomic, strong) MPMoviePlayerController *player; 8 - (IBAction)play; 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 } 17 18 - (MPMoviePlayerController *)player 19 { 20 if (_player == nil) { 21 NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"]; 22 23 _player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 24 25 // 设置控制器View所在的位置 26 _player.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16); 27 28 // 设置播放器的控制模式 29 _player.controlStyle = MPMovieControlStyleFullscreen; 30 31 [self.view addSubview:self.player.view]; 32 } 33 return _player; 34 } 35 36 - (IBAction)play { 37 [self.player play]; 38 } 39 40 @end
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步