有那么一些时候,我们只需要简单的播放一些小视频,本地的或者网上的资源,不需要各种炫酷的效果,不需要自己各种控制,只是想安安静静的播放完,退出!网上各种开源的封装的AVPlayer的开源库,各有千秋,但是集成进来有感觉动静太大了,大把大把的控件和控制代理等等,头都大了!对于我这种菜逼,慌得一批~~所以我就在系统提供的AVPlayerViewController动起了手脚!
#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, strong) NSString *videoUrl;
@property (nonatomic, strong)AVPlayerViewController *playerVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.videoUrl = [[NSBundle mainBundle] pathForResource:@"guideMovie1" ofType:@"mov"];
self.videoUrl = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
/*
因为是 http 的链接,所以要去 info.plist里面设置
App Transport Security Settings
Allow Arbitrary Loads = YES
*/
self.playerVC = [[AVPlayerViewController alloc] init];
self.playerVC.player = [AVPlayer playerWithURL:[self.videoUrl hasPrefix:@"http"] ? [NSURL URLWithString:self.videoUrl]:[NSURL fileURLWithPath:self.videoUrl]];
self.playerVC.view.frame = self.view.bounds;
self.playerVC.showsPlaybackControls = YES;
//self.playerVC.entersFullScreenWhenPlaybackBegins = YES;//开启这个播放的时候支持(全屏)横竖屏哦
//self.playerVC.exitsFullScreenWhenPlaybackEnds = YES;//开启这个所有 item 播放完毕可以退出全屏
[self.view addSubview:self.playerVC.view];
if (self.playerVC.readyForDisplay) {
[self.playerVC.player play];
}
}
@end
就是这么简单,我们就可以播放网络或者本地视频啦,简洁大气上档次,还是暗黑风格哦,如果项目开启了屏幕方向,自动支持横竖屏切换,美滋滋!上两张图来占占篇幅哈!!
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/gaotouavplayerviewcontrollerbaichuwoxiangyaodezishi.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:1 月 3, 2019 at 01:37 下午