创建播放器(四种方法)
URL创建的方式会默认为AVPlayer创建一个AVPlayerItem
self.player = [AVPlayer playerWithURL:localVideoUrl]
self.player = [[AVPlayer alloc] initWithURL:localVideoUrl]
self.player = [AVPlayer playerWithPlayerItem:playerItem]
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem]
#import "ViewController.h"
#import <AVKit/AVKit.h>
@interface ViewController ()
@property (nonatomic,strong) AVPlayerItem *item;
@property (nonatomic,strong) AVPlayerLayer *playerLayer;
@property (nonatomic,strong) AVPlayer *myPlayer;
@property (strong, nonatomic) UISlider *avSlider;
@property (assign, nonatomic) BOOL isReadToPlay;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self avPlayerMethod];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(250, 600, 100, 100);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.avSlider addTarget:self action:@selector(avSliderAction) forControlEvents:
UIControlEventTouchUpInside|UIControlEventTouchCancel|UIControlEventTouchUpOutside];
}
- (void)avPlayerMethod {
NSURL *mediaURL = [NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
self.item = [AVPlayerItem playerItemWithURL:mediaURL];
self.myPlayer = [AVPlayer playerWithPlayerItem:self.item];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.myPlayer];
self.playerLayer.frame = CGRectMake(0, 66, self.view.bounds.size.width, 300);
[self.view.layer addSublayer:self.playerLayer];
[self.item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:
(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
AVPlayerItemStatus status = [change[NSKeyValueChangeNewKey]intValue];
switch (status) {
case AVPlayerItemStatusFailed:
NSLog(@"item 有误");
self.isReadToPlay = NO;
break;
case AVPlayerItemStatusReadyToPlay:
NSLog(@"准好播放了");
self.isReadToPlay = YES;
self.avSlider.maximumValue = self.item.duration.value / self.item.duration.timescale;
break;
case AVPlayerItemStatusUnknown:
NSLog(@"视频资源出现未知错误");
self.isReadToPlay = NO;
break;
default:
break;
}
}
[object removeObserver:self forKeyPath:@"status"];
}
- (void)playAction {
if (self.isReadToPlay) {
[self.myPlayer play];
}else{
NSLog(@"视频正在加载中");
}
}
- (UISlider *)avSlider {
if (!_avSlider) {
_avSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 355, self.view.bounds.size.width, 30)];
[self.view addSubview:_avSlider];
}
return _avSlider;
}
- (void)avSliderAction {
float seconds = self.avSlider.value;
CMTime startTime = CMTimeMakeWithSeconds(seconds, self.item.currentTime.timescale);
[self.myPlayer seekToTime:startTime completionHandler:^(BOOL finished) {
if (finished) {
[self playAction];
}
}];
}
@end
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController( )
@property (strong, nonatomic) AVPlayer *avPlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"视频播放";
NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
NSURL *url = [NSURL URLWithString:playString];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];
layer.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 230);
layer.backgroundColor = [UIColor cyanColor].CGColor;
layer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:layer];
[self.avPlayer play];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
@end
@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
[self.view addSubview:self.playerVC.view]
if (self.playerVC.readyForDisplay) {
[self.playerVC.player play]
}
}
@end