使用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資源創(chuàng)建一個播放器
self.player = [[AVPlayer alloc] initWithURL:remoteURL];
// 2.根據播放器對象, 創(chuàng)建一個圖層, 展示視頻內容給用戶看
_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.創(chuàng)建播放器對象
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