#import "ViewController.h"
#import
#import
/
/
static const NSString PlayerItemStatusContext;
@interface ViewController ()
@property (nonatomic, strong) AVPlayer player;
@end
@implementation ViewController
- (void)viewDidLoad {
【super viewDidLoad】;
// //1. 根据网址创建AVPlayer
// self.player = 【AVPlayer playerWithURL:【NSURL URLWithString:@""】】;
//
// //2. 创建PlayerLayer
// AVPlayerLayer playerLayer = 【AVPlayerLayer playerLayerWithPlayer:self.player】;
//
// //3. 设置大小
// playerLayer.frame = self.view.bounds;
//
// //4. 添加到layer中
// 【self.view.layer addSublayer:playerLayer】;
//
// //5. 播放
// 【self.player play】;
//
}
- (IBAction)playLocalVideoClick:(id)sender {
NSURL assetURL = 【【NSBundle mainBundle】 URLForResource:@"02开发环境.mp4" withExtension:nil】;
//
// AVAsset asset = 【AVAsset assetWithURL:assetURL】;
//
// AVPlayerItem playerItem = 【AVPlayerItem playerItemWithAsset:asset】;
//
// 【playerItem addObserver:self forKeyPath:@"status" options:0 context:PlayerItemStatusContext】;
//
// self.player = 【AVPlayer playerWithPlayerItem:playerItem】;
self.player = 【AVPlayer playerWithURL:assetURL】;
AVPlayerLayer playerLayer = 【AVPlayerLayer playerLayerWithPlayer:self.player】;
playerLayer.frame = self.view.bounds;
【self.view.layer addSublayer:playerLayer】;
【self.player play】;
}
- (IBAction)playRomoteVideoClick:(id)sender {
//1. AVPlayerItem会创建媒体资源动态视角的数据模型(比如当前播放时间, 实现时间跳转等), 并保存AVPlayer在播放资源时的呈现状态
AVPlayerItem playerItem = 【AVPlayerItem playerItemWithURL:【NSURL URLWithString:@""】】;
【playerItem addObserver:self forKeyPath:@"status" options:0 context:PlayerItemStatusContext】;
self.player = 【AVPlayer playerWithPlayerItem:playerItem】;
AVPlayerLayer playerLayer = 【AVPlayerLayer playerLayerWithPlayer:self.player】;
playerLayer.frame = self.view.bounds;
【self.view.layer addSublayer:playerLayer】;
}
- (void)observeValueForKeyPath:(NSString )keyPath ofObject:(id)object change:(NSDictionary<NSString ,id
{
if (context == PlayerItemStatusContext) {
AVPlayerItem playerItem = (AVPlayerItem )object;
if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
【self.player play】;
}
}
}
- (IBAction)mpVCPlayRomoteClick:(id)sender {
MPMoviePlayerViewController mpvc = 【【MPMoviePlayerViewController alloc】 initWithContentURL:【NSURL URLWithString:@""】】;
【self presentModalViewController:mpvc animated:YES】;
}//代码效果参考:http://www.ezhiqi.com/zx/art_3244.html
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
// 【self.player play】;
}//代码效果参考:http://www.ezhiqi.com/bx/art_963.html
这是一个小demo的实现
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property(nonatomic,strong)AVPlayer player;//播放对象
@property(nonatomic,strong)MPMoviePlayerController *mc;
@end
@implementation ViewController
- (void)viewDidLoad {
【super viewDidLoad】;
// Do any additional setup after loading the view, typically from a nib.
}//代码效果参考:http://www.ezhiqi.com/zx/art_2750.html
/
/
- (IBAction)clik:(id)sender {
//1mpc:
// self.mc=【【MPMoviePlayerController alloc】initWithContentURL:【NSURL URLWithString:@""】】;
// self.mc.view.frame=CGRectMake(0, 200, 375, 200);
// 【self.view addSubview:self.mc.view】;
// 【self.mc play】;
//2mpvc
// MPMoviePlayerViewController mpv=【【MPMoviePlayerViewController alloc】initWithContentURL:【NSURL URLWithString:@""】】;
// 【self presentViewController:mpv animated:YES completion:nil】;
//3Aplayer
//根据网址创建
self.player=【AVPlayer playerWithURL:【NSURL URLWithString:@""】】;
AVPlayerLayer *player=【AVPlayerLayer playerLayerWithPlayer:self.player】;
player.frame=self.view.bounds;
【self.view.layer addSublayer:player】;
//播放
【self.player play】;
}