
#import "ViewController.h"#import#import@interface ViewController (){ AVPlayer * _player; AVPlayerViewController * _playerViewController; }@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
NSString *str=@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
NSURL *url=[NSURL URLWithString:str];
// 3、配置媒体播放控制器
_playerViewController = [[AVPlayerViewController alloc] init];
// 设置媒体源数据
_playerViewController.player = [AVPlayer playerWithURL:url];
// 设置拉伸模式
_playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
// 设置是否显示媒体播放组件
_playerViewController.showsPlaybackControls = YES;
// 设置代理
// _playerViewController.delegate = self;
// 播放视频
[_playerViewController.player play];
// 设置媒体播放器视图大小
_playerViewController.view.bounds = CGRectMake(0, 0, 475, 250);
_playerViewController.view.center = CGPointMake(CGRectGetMidX(self.view.bounds),44+ CGRectGetMidY(_playerViewController.view.bounds) );
// 4、推送播放
// 推送至媒体播放器进行播放
// [self presentViewController:_playerViewController animated:YES completion:nil];
// 直接在本视图控制器播放
[self addChildViewController:_playerViewController];
[self.view addSubview:_playerViewController.view];
}
网友评论