在iOS中,与视频相关的接口有5个,从顶层开始分别是 AVKit - AVFoundation - VideoToolbox - Core Media - Core Video
=============================================================================
AVKit中的AVPlayviewcontroller实现播放。
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface playVC ()
@end
@implementation playVC
- (void)viewDidLoad {
[superviewDidLoad];
self.navigationItem.title=@"播放器页面";
self.view.backgroundColor=[UIColorwhiteColor];
[selfaddbackBtn];
NSString *path = [[NSBundlemainBundle]pathForResource:@"test.mov"ofType:nil];
NSURL *url = [NSURLfileURLWithPath:path];
AVPlayerViewController *playerVc = [[AVPlayerViewControlleralloc]init];
playerVc.player = [[AVPlayeralloc]initWithURL:url];
[selfpresentViewController:playerVcanimated:YEScompletion:nil];
}
======================
画中画效果-------只有在IPAd 上可以用
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVPlayerViewControllerDelegate>
- (IBAction)playClick:(id)sender;
@property (nonatomic,strong)AVPlayerViewController *playerVc;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
NSString *path = [[NSBundlemainBundle]pathForResource:@"test.mov"ofType:nil];
NSURL *url = [NSURLfileURLWithPath:path];
AVPlayerViewController *playerVc = [[AVPlayerViewControlleralloc]init];
self.playerVc = playerVc;
playerVc.player = [[AVPlayer alloc]initWithURL:url];//AVPlayerViewController有一个AVPlayer的属性;
playerVc.delegate = self;
//画中画
//1.开启 2.属性支持画中画 3.iPad air2之后的新的
playerVc.allowsPictureInPicturePlayback =YES;
}
- (IBAction)playClick:(id)sender {
[selfpresentViewController:self.playerVcanimated:YEScompletion:nil];
//[self.playerVc.player play];
}
/*!
@method playerViewControllerWillStartPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will start.
*/
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;{
NSLog(@"playerViewControllerWillStartPictureInPicture");
}
/*!
@method playerViewControllerDidStartPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did start.
*/
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;{
NSLog(@"playerViewControllerDidStartPictureInPicture");
}
/*!
@method playerViewController:failedToStartPictureInPictureWithError:
@param playerViewController
The player view controller.
@param error
An error describing why it failed.
@abstract Delegate can implement this method to be notified when Picture in Picture failed to start.
*/
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;{
NSLog(@"failedToStartPictureInPictureWithError");
}
/*!
@method playerViewControllerWillStopPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will stop.
*/
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;{
NSLog(@"playerViewControllerWillStopPictureInPicture");
}
/*!
@method playerViewControllerDidStopPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did stop.
*/
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;{
NSLog(@"playerViewControllerDidStopPictureInPicture");
}
/*!
@method playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method and return NO to prevent player view controller from automatically being dismissed when Picture in Picture starts.
*/
- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController;{
return NO;
}
/*!
@method playerViewController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:
@param playerViewController
The player view controller.
@param completionHandler
The completion handler the delegate needs to call after restore.
@abstract Delegate can implement this method to restore the user interface before Picture in Picture stops.
*/
- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;{
completionHandler(YES);
}
@end
***************
AVFoundation中的AVPlayer实现播放
//读取本地视频路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"water" ofType:@"MOV"];
//为即将播放的视频内容进行建模
AVPlayerItem *avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
//创建监听(这是一种KOV的监听模式)
[avplayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//给播放器赋值要播放的对象模型
AVPlayer *avplayer = [AVPlayer playerWithPlayerItem:avplayerItem];
//指定显示的Layer
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avplayer];
layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
[self.view.layer addSublayer:layer];
KVO监听
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
AVPlayerItem *item = object;
//判断监听对象的状态
if ([keyPath isEqualToString:@"status"]) {
if (item.status == AVPlayerItemStatusReadyToPlay) {//准备好的
NSLog(@"AVPlayerItemStatusReadyToPlay");
} else if(item.status ==AVPlayerItemStatusUnknown){//未知的状态
NSLog(@"AVPlayerItemStatusUnknown");
}else if(item.status ==AVPlayerItemStatusFailed){//有错误的
NSLog(@"AVPlayerItemStatusFailed");
}
}
}