iOS - AVPlayer 音视频播放
前言
NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject
@available(iOS 4.0, *) public class AVPlayer : NSObject
NS_CLASS_AVAILABLE_IOS(8_0) @interface AVPlayerViewController : UIViewController
@available(iOS 8.0, *) public class AVPlayerViewController : UIViewController
AVPlayer 既可以播放音乐又可以播放视频;使用 AVPlayer 不能直接显示视频,必须要加入 AVPlayerLayer 中,并添加到其他能显示的 layer 中,AVPlayer 播放界面中不带播放控件。
- MediaPlayer 的影片是放在 UIView 里面,而 AVPlayer 是放在 AVPlayerLayer 里面,AVPlayerLayer 是 CALayer 的子类別。
- 使用 MediaPlayer 前,要记得加入 MediaPlayer.framework 及 #import <MediaPlayer/MediaPlayer.h>
- 使用 AVPlayer 前,要记得加入 AVFoundation.framework 及 #import <AVFoundation/AVFoundation.h>
MeidaPlayer 框架中的 MPMoviePlayerController 类和 MPMoviePlayerViewController 类是 iOS 中视频播放的开发相关类和方法。在 iOS8 中,iOS 开发框架中引入了一个新的视频框架 AVKit,其中提供了视频开发类 AVPlayerViewController 用于在应用中嵌入播放视频的控件。AVPlayerViewcontroller 继承自 UIViewController,一般适用于点击一个视频缩略图,modal 出一个新的界面来进行播 放的情况,AVPlayerViewcontroller 既可以播放音乐又可以播放视频,播放界面中自带播放控件。在 iOS8 中,这两个框架中的视频播放功能并无太大差异,基本都可以满足开发者的需求。iOS9 系统后,iPad Air 正式开始支持多任务与画中画的分屏功能,所谓画中画,即是用户可以将当前播放的视频缩小放在屏幕上同时进行其他应用程序的使用。这个革命性的功能将极大的方便用户的使用。于此同时,在 iOS9 中,MPMoviePlayerController 与 MPMoviePlayerViewController 类也被完全弃用,开发者使用 AVPlayerViewController 可以十分方便的实现视频播放的功能并在一些型号的 iPad 上集成画中画的功能。
- 实现画中画要实现以下三步:
- 1、确保当前调试版本在 9.0 以上。
- 2、在项目 TARGETS 中的 Capabilities -> Gapabilities -> Background Mode(选项开关打开)-> 勾选 Audio, AirPlay and Picture in Picture
3、设置 AVAudioSession,这两行必须设置,不然画中画不能用,如果不写模拟器中可以画中画,但是在 iPad 设备中不能。
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
播放设置:
使用 AVPlayer 播放:
添加库文件:AVFoundation.framework 包含头文件:#import <AVFoundation/AVFoundation.h>
使用 AVPlayerViewController 播放:
添加库文件:AVKit.framework AVFoundation.framework 包含头文件:#import <AVKit/AVKit.h> #import <AVFoundation/AVFoundation.h>
1、本地/网络音视频播放
1.1 使用 AVPlayer 播放
Objective-C
添加库文件:AVFoundation.framework 包含头文件:#import <AVFoundation/AVFoundation.h>
直接由 URL 创建
// 加载本地音乐 NSURL *movieUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"蓝莲花" ofType:@"mp3"]]; // 加载本地视频 NSURL *movieUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"步步高手机" ofType:@"mp4"]]; // 加载网络视频 NSURL *movieUrl = [NSURL URLWithString:@"http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4"]; // 创建 AVPlayer 播放器 AVPlayer *player = [AVPlayer playerWithURL:movieUrl]; // 将 AVPlayer 添加到 AVPlayerLayer 上 AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; // 设置播放页面大小 playerLayer.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 200); // 设置画面缩放模式 playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; // 在视图上添加播放器 [self.view.layer addSublayer:playerLayer]; // 开始播放 [player play];
由 AVPlayerItem 创建
// 加载本地音乐 NSURL *movieUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"蓝莲花" ofType:@"mp3"]]; // 加载本地视频 NSURL *movieUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"步步高手机" ofType:@"mp4"]]; // 加载网络视频 NSURL *movieUrl = [NSURL URLWithString:@"http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4"]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:movieUrl]; // 创建 AVPlayer 播放器 AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; // 将 AVPlayer 添加到 AVPlayerLayer 上 AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; // 设置播放页面大小 playerLayer.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 200); // 设置画面缩放模式 playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; // 在视图上添加播放器 [self.view.layer addSublayer:playerLayer]; // 开始播放 [player play];
Swift
添加库文件:AVFoundation.framework 包含头文件:import AVFoundation
直接由 URL 创建
// 加载本地音乐 let movieUrl:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("蓝莲花", ofType: "mp3")!) // 加载本地视频 let movieUrl:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("步步高手机", ofType: "mp4")!) // 加载网络视频 let movieUrl:NSURL = NSURL(string: "http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4")! // 创建 AVPlayer 播放器 let player:AVPlayer = AVPlayer(URL: movieUrl) // 将 AVPlayer 添加到 AVPlayerLayer 上 let playerLayer:AVPlayerLayer = AVPlayerLayer(player: player) // 设置播放页面大小 playerLayer.frame = CGRectMake(10,