iOS 媒体功能主要的类
在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来
+ (instancetype)playerWithURL:(NSURL *)URL;
+ (instancetype)playerWithPlayerItem:(AVPlayerItem *)item;
- (instancetype)initWithURL:(NSURL *)URL;
- (instancetype)initWithPlayerItem:(AVPlayerItem *)item;
// 1、播放
- (void)play;
// 2、暂停
- (void)pause;
// 1、遍历初始化
+ (instancetype)layer;
// 2、alloc + init
// 3、根据播放媒体初始化
+ (AVPlayerLayer *)playerLayerWithPlayer:(nullable AVPlayer *)player;
// 1、即将开始画中画
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;
// 2、开始画中画
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;
// 3、画中画失败
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;
// 4、即将结束画中画
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;
// 5、结束画中画
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;
//
// ViewController.m
// videotest
//
// Created by 谢厂节 on 2017/9/12.
// Copyright © 2017年 谢厂节. All rights reserved.
//
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
enum {
DirectPlayBtnTag = 10,
FullScreenPlayBtnTag
};
@interface ViewController () <AVPlayerViewControllerDelegate> {
AVPlayer *_player; /**< 媒体播放器 */
AVPlayerViewController *_playerViewController; /**< 媒体播放控制器 */
}
@property (nonatomic, strong) UIButton *directPlayBtn; /**< 直接播放按钮 */
@property (nonatomic, strong) UIButton *fullscreenPlayBtn; /**< 全屏播放 */
@property (nonatomic, strong) AVPlayer *player; /**< 媒体播放器 */
@property (nonatomic, strong) AVPlayerViewController *playerViewController; /**< 媒体播放控制器 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeUserInterface];
}
#pragma mark *** Initialize methods ***
- (void)initializeUserInterface {
self.title = @"音乐播放器";
self.view.backgroundColor = [UIColor whiteColor];
// 加载视图
[self.view addSubview:self.directPlayBtn];
[self.view addSubview:self.fullscreenPlayBtn];
}
#pragma mark *** Events ***
- (void)respondsToButton:(UIButton *)sender {
switch (sender.tag) {
// 直接播放
case DirectPlayBtnTag: {
if (_player) {
[_player pause];
_player = nil;
}
if (_playerViewController) {
[_playerViewController removeFromParentViewController];
[_playerViewController.view removeFromSuperview];
_playerViewController = nil;
}
// 1、获取媒体资源地址
NSString *path = [[NSBundle mainBundle] pathForResource:@"assets/2-0" ofType:@"mp4"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:path];
// 2、创建AVPlayerItem
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:sourceMovieURL];
// 3、根据AVPlayerItem创建媒体播放器
_player = [AVPlayer playerWithPlayerItem:playerItem];
// 4、创建AVPlayerLayer,用于呈现视频
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
// 5、设置显示大小和位置
playerLayer.bounds = CGRectMake(0, 0, 350, 300);
playerLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(playerLayer.bounds) + 30);
// 6、设置拉伸模式
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
// 7、获取播放持续时间
NSLog(@"%lld", playerItem.duration.value);
[_player play];
[self.view.layer addSublayer:playerLayer];
}
break;
// 全屏播放
case FullScreenPlayBtnTag: {
if (_player) {
[_player pause];
_player = nil;
}
// 初始化URL资源地址
// 获取网络资源地址
// + (nullable instancetype)URLWithString:(NSString *)URLString;
// 1、获取本地资源地址
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"宣传资料.mp4"]];
// 2、初始化媒体播放控制器
if (_playerViewController) {
_playerViewController = nil;
}
// 3、配置媒体播放控制器
_playerViewController = [[AVPlayerViewController alloc] init];
// 设置媒体源数据
_playerViewController.player = [AVPlayer playerWithURL:url];
// 设置拉伸模式
_playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
// 设置是否显示媒体播放组件
_playerViewController.showsPlaybackControls = YES;
// 设置代理
_playerViewController.delegate = self;
// 播放视频
[_playerViewController.player play];
// 设置媒体播放器视图大小
_playerViewController.view.bounds = CGRectMake(0, 0, 350, 300);
_playerViewController.view.center = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(_playerViewController.view.bounds) + 30);
// 4、推送播放
// 推送至媒体播放器进行播放
// [self presentViewController:_playerViewController animated:YES completion:nil];
// 直接在本视图控制器播放
[self addChildViewController:_playerViewController];
[self.view addSubview:_playerViewController.view];
}
break;
default:
break;
}
}
#pragma mark *** AVPlayerViewControllerDelegate ***
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
#pragma mark *** Getters ***
- (UIButton *)directPlayBtn {
if (!_directPlayBtn) {
_directPlayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_directPlayBtn.bounds = CGRectMake(0, 0, 100, 40);
_directPlayBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) - CGRectGetMidX(_directPlayBtn.bounds), CGRectGetMidY(self.view.bounds) + 100);
_directPlayBtn.tag = DirectPlayBtnTag;
[_directPlayBtn setTitle:@"直接播放" forState:UIControlStateNormal];
[_directPlayBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _directPlayBtn;
}
- (UIButton *)fullscreenPlayBtn {
if (!_fullscreenPlayBtn) {
_fullscreenPlayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_fullscreenPlayBtn.bounds = CGRectMake(0, 0, 100, 30);
_fullscreenPlayBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) + CGRectGetMidX(_fullscreenPlayBtn.bounds), CGRectGetMidY(self.view.bounds) + 100);
_fullscreenPlayBtn.tag = FullScreenPlayBtnTag;
[_fullscreenPlayBtn setTitle:@"全屏播放" forState:UIControlStateNormal];
[_fullscreenPlayBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _fullscreenPlayBtn;
}
@end
文章内容转自:http://blog.csdn.net/hierarch_lee/article/details/47903979