iOS AVPlayer 与 AVPlayerViewController
来源:互联网 发布:软件项目监理方案 编辑:程序博客网 时间:2024/05/21 18:19
iOS 媒体功能主要的类
- AVPlayer 全功能影音播放器
- AVPlayerLayer 主要用于显示视频
- AVPlayerViewController 提供了默认的可视化控制界面
一、 AVPlayer
1. 说明
- 使用AVPlayer需导入AVFoundation.h。
- 支持视频格式: WMV,AVI,MKV,RMVB,RM,XVID,MP4,3GP,MPG等。
- 支持音频格式:MP3,WMA,RM,ACC,OGG,APE,FLAC,FLV等。
在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来
- AVPlayer并未提供视频操作组件,需用户自定义。
2. 初始化方法
+ (instancetype)playerWithURL:(NSURL *)URL;+ (instancetype)playerWithPlayerItem:(AVPlayerItem *)item;- (instancetype)initWithURL:(NSURL *)URL;- (instancetype)initWithPlayerItem:(AVPlayerItem *)item;
3. 属性
- status:获取播放媒体状态
- rate:设置播放速度
4. 常用方法
// 1、播放- (void)play;// 2、暂停- (void)pause;
二、 AVPlayerLayer
1. 初始化方法
// 1、遍历初始化+ (instancetype)layer;// 2、alloc + init// 3、根据播放媒体初始化+ (AVPlayerLayer *)playerLayerWithPlayer:(nullable AVPlayer *)player;
2. 常用属性
- bounds:设置播放矩形区域;
- position:设置播放区域中心点位置;
- videoGravity:设置拉伸模式
- player:设置播放媒体
三、 AVPlayerViewController
- 要使用AVPlayerViewController需导入AVKit.h。
- AVPlayerViewController整合了一个完整的播放器,可以作为控制器进行操作显示。
- 支持播放本地及网络视频文件
1. 常用属性
- player:设置播放器
- showsPlaybackControls:设置是否显示媒体播放组件,默认YES
- videoGravity:设置视频拉伸模式
- allowsPictureInPicturePlayback:设置是否允许画中画回放,默认YES
- delegate:设置代理
四、 AVPlayerViewControllerDelegate
// 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
阅读全文
0 0
- iOS AVPlayer 与 AVPlayerViewController
- 媒体播放器 AVPlayer 与 AVPlayerViewController
- AVPlayer与AVPlayerViewController的使用
- iOS AVAudioPlayer vs. AVPlayer AVPlayerViewController vs.MPMoviePlayerViewController
- OCiOS开发:媒体播放器 AVPlayer 与 AVPlayerViewController
- 视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller) for iOS
- 视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller) for iOS
- iOS之AVPlayer、MPMoviePlayerController、MPMoviePlayerViewController、AVPlayerViewController播放音乐和视频
- 简单视频播放AVPlayer和AVPlayerViewController
- ios-AVPlayerViewController简单使用
- 使用AVPlayerViewController和AVPlayer播放视频 和 画中画(pictureInPicture)
- iOS开发 AVPlayerViewController(视频播放)的使用
- iOS AVplayer 基本使用方法
- iOS AVPlayer 使用详解
- iOS AVPlayer音频开发
- ffmpeg-avplayer-for-ios
- iOS 开发 AVPlayer 深入浅出
- AvPlayer与AVPlayerItem
- U盘病毒
- Ubuntu10.04下安装json-c_12
- 【bzoj5016】[Snoi2017]一个简单的询问(莫队)
- hdu2553 N皇后问题(深搜)
- Linux下ll 命令显示的文件类型
- iOS AVPlayer 与 AVPlayerViewController
- 【SHOI 2002】百事世界杯之旅 概率期望
- 互联网时代用什么来拯救你的眼睛?
- html5自带表单验证体验优化及提示气泡修改
- .Net中Schema的使用
- Swift 函数与闭包
- jQuery ajax普通情况 提交表单情况
- 基于Swift的iOS应用程序开发:使用UIImagePickerController调取摄像头进行拍照
- Java学习笔记—集合