使用AVPlayer 播放远程视频
ViewController
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;
/** 图层*/
@property (nonatomic, weak) AVPlayerLayer *layer;
@end
@implementation ViewController
#pragma mark - 懒加载
- (AVPlayerLayer *)layer
{
if (_layer == nil) {
// 获取远程视频资源
// 0.获取远程视频url
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
// 1.根据url资源创建一个播放器
self.player = [[AVPlayer alloc] initWithURL:remoteURL];
// 2.根据播放器对象, 创建一个图层, 展示视频内容给用户看
_layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
}
return _layer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:self.layer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.player play];
}
// 不加下面的方法,虽然可以放视频,可以听到声音
// 但是不能看到画面,得添加到相应的图层上去
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.layer.frame = self.view.bounds;
}
@end
使用MPMoviePlayerViewController播放视频
ViewController
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/** 播放器*/
@property (nonatomic, strong) MPMoviePlayerController *pc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 0.获取资源路径
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
// 1.创建播放器对象
self.pc = [[MPMoviePlayerController alloc] initWithContentURL:remoteURL];
[self.view addSubview:self.pc.view];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.pc play];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.pc.view.frame = self.view.bounds;
}
@end
使用MPMoviePlayerViewController播放视频
ViewController
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/** 播放器 */
@property (nonatomic ,strong) MPMoviePlayerViewController *pvc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 0.获取远程视频url
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
self.pvc = [[MPMoviePlayerViewController alloc] initWithContentURL:remoteURL];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.presentedViewController) { // 已经有指向的控制器了
return;
}
[self presentViewController:self.pvc animated:YES completion:^{
[self.pvc.moviePlayer play];
}];
}
@end
iOS9.0之后, 需要使用AVPlayerViewController
ViewController
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** 播放器 */
@property (nonatomic ,strong) AVPlayerViewController *playerVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.playerVC = [[AVPlayerViewController alloc] init];
// 0.获取远程视频url
NSURL *remoteURL = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
AVPlayer *player = [[AVPlayer alloc] initWithURL:remoteURL];
self.playerVC.player = player;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.presentedViewController) { // 已经有指向的控制器
return;
}
[self presentViewController:self.playerVC animated:YES completion:^{
[self.playerVC.player play];
}];
}
@end