1.iOS多模式&富交互视频播放器TTAVPlayer(附源码)
2.å¦ä½å¼åä¸ä¸ª AVPlayer iPad ç¨åº
iOS多模式&富交互视频播放器TTAVPlayer(附源码)
源码可见:[直接点击]
为了提升播放器的源码交互友好性与适应多样化业务需求,决定对播放器进行优化。分析
本次优化主要目标是源码实现四个播放模式:普通模式、竖屏模式、分析微信见缝插针源码横屏模式、源码静音模式。分析
普通模式用于商品内容和文章内容的源码嵌套播放,H5桥接播放。分析竖屏模式提供沉浸式体验,源码用于H5桥接播放与静音播放时的分析点击查看详情场景,如微博、源码蚂蚁朔源码手淘微淘。分析横屏模式追求最佳播放体验,源码提供丰富的分析交互操作,如快捷音量调节、源码播放进度调整和屏幕亮度控制。零负源码静音模式适用于列表自动播放场景,如手淘微淘列表。
最终实现效果包括不同模式的支持,以及基础功能和自定义模式的扩展性设计。
设计思路强调“最小接入成本与最大扩展性”。爱帮帮源码在API和Framework层面,提供简单易用的接口,同时为自定义需求提供强大的能力。
播放器设计包含五种模式,基于AVPlayer实现基本播放功能,赈灾金源码同时创建TTAVPlayerView用于展示视频及UI控件。
TTAVPlayer负责播放控制与状态回调,TTAVPlayerView面向上层调用者,提供界面渲染和业务逻辑。
实现中,设计确保TTAVPlayerView代码的纯净性,与业务代码解耦,专注于视频播放。
播放器提供丰富功能,包括手势识别、自动横竖屏切换、网络切换检测、静音播放等。
优化成果旨在提升播放器的交互体验与适应性,欢迎提供反馈、报告Bug或提出新功能需求。
获取源码、Demo与****:
知乎:[直接点击]
GitHub:[直接点击]
简书:[直接点击]
å¦ä½å¼åä¸ä¸ª AVPlayer iPad ç¨åº
AirPlay 并ä¸æ¯æ°ä¸è¥¿ï¼å®å¨ iOS 4.3 SDK ä¸å°±åå¨äºãä½AirPlay API ä¸ä¸æææ°çä¸è¥¿å å ¥ãå ¶ä¸ä¸ä¸ªæ趣æ°ç¹æ§æ¯ iPad ç¨åºè½éè¿ Apple TV 2 ææ¾æ¥èª iOS 设å¤ä¸çå 容ï¼æè éåiPad 2 ä¸çå±å¹å 容ã
ç®èè¨ä¹ï¼AirPlay å°±æ¯å°åªä½å 容ææ¾å°é«æ¸ æ¾ç¤ºå¨ï¼çµè§ï¼æé«æ¸ é³æç³»ç»ä¸ãApple TV 2 åæ¯ç¨äºè¿æ¥ iOS 设å¤æ iTunesä¸é«æ¸ 设å¤ä¹é´çæ¡¥æ¢ã
éç iOS 5 çåºç°ï¼ä»»ä½ä½¿ç¨ AV Foundation ç±»çç¨åºé½è½ä»ç¨åºä¸ææ¾é³è§é¢å å®¹å° Apple TVãä½ å¯ä»¥ç¨MPMoviewPlayerController éè¿ AirPlay å°å½åæ£å¨ææ¾çå 容æå½±å°é«æ¸ çµè§æå ¶å®é«æ¸ æ¾ç¤ºè®¾å¤ä¸ãå¦ä¸ä¸ªè¿æ¥æ¯ä»UIWebView ææ¾è§é¢ï¼è¿æ¯æ¿å¨äººå¿çæ¹è¿ï¼å 为è¿æå³çæ们å¯ä»¥ç´æ¥ä» web ä¸å°å¨çº¿çé³è§é¢æå½±å°çµè§æè Apple TV 2 ä¸ã
ä½¿ç¨ AVFoundation æå½±è§é¢å 容
è¦å¨åºç¨ç¨åºä¸ä½¿ç¨ AVFoundationï¼éè¦å®ç° AVPlayer 并设置 allowsAirPlayVideo 为 YES 以å¼å¯ AirPlayï¼æè 设置为NO ä»¥å ³é AirPlayï¼å¦ä»¥ä¸ä»£ç æ示ï¼
-(BOOL)setAirPlay:(BOOL)airplayMode{
return self.player.allowsAirPlayVideo=airplayMode;
}
ç¼ååºç¨ç¨åº
为äºæ¼ç¤ºå¦ä½å建 AVPlayer åºç¨ç¨åºä»¥åå®ç° AirPlayï¼æ们å°å建ä¸ä¸ª Single View Application(注æå¾é UseStoryboards)ï¼æ°å»ºä¸ä¸ª AVPlayer 类并å®ç° AirPlay ç¹æ§ã
å建ä¸ä¸ª Single View Applicationãç®æ ç±»åå¯ä»¥éæ© iPhoneãiPod æè iPadãå建项ç®ä¹åï¼åå¯¼å ¥ AV Foundation æ¡æ¶ã
æ¥çå建ä¸ä¸ªæ° classï¼å½å为 Playerï¼å¹¶ç»§æ¿ UIView ç±»ãå¨å¤´æ件ä¸ï¼å å ¥ AVPlayer 类并å¢å ä¸ä¸ª AVPlayer å±æ§ãå¦ä¸å代ç æ示ã
@class AVPlayer;
@interface Player : UIView
@property(nonatomic, strong) AVPlayer * player;
æ¥ä¸æ¥å°±æ¯å®ç° Player ç±»ã
æ们å®ç°äºæåºæ¬ç AVPlayer éè¦å®ç°çæèµ·ç ç 4 个æ¹æ³ï¼åæ¶è¿æä¾äºä¸ä¸ªæ¹æ³ä½ä¸ºæ们ç AirPlay å¼å ³ã
é¦å æ们éè¦ä¸ä¸ª AVLayer ç±»çå è£ ç±»ã该类æ¯ä¸ä¸ª CALayer åç±»ï¼ç¨äºå¯¹åªä½çå¯è§å 容è¿è¡ç®¡çãå建å è£ ç±»ç代ç å¦ä¸ï¼
+ (Class)layerClass {
return [AVPlayerLayer class];
}
ç¶å为éè¦ä¸ä¸ªæ¹æ³ï¼å®ä¾åä¸ä¸ª AVPlayer 对象ï¼æ们å¨å¤´æ件ä¸å®ä¹çï¼ãå¦ä¸å代ç æ示ã
-(AVPlayer *) player{
return [(AVPlayerLayer *)[self layer] player];
}
å¨ setPlayer æ¹æ³ä¸ï¼å¦ä¸å代ç æ示ï¼æä¸ä¸ª AVPlayer åæ°ï¼ç¨äºå°ä¸ä¸ª AVPlayer å®ä¾æ·»å å° UIViewãè¿ä¸ª UIView åç±»ï¼å°ç¨å¨ä¸» View Controller ä¸ã
- (void)setPlayer:(AVPlayer*)player {
[(AVPlayerLayer*)[self layer] setPlayer:player];
}
è¿ä¸ªç±»çæåä¸ä¸ªæ¹æ³æ¯ setAirPlay æ¹æ³ãå ¶åæ°å°ç¨äºæå® AVPlayer UIView (Player)çallowsAirPlayVideo å±æ§ãå¦ä»¥ä¸ä»£ç æ示ã
-(BOOL)setAirPlay:(BOOL)airplayMode{
return self.player.allowsAirPlayVideo=airplayMode;
}
为è§é¢çè¾åºæå®å¾å±(AVPLayerLayerï¼æ¶ï¼å¯ä»¥æå®ä»»ææ°éçå¾å±,åªè¦è½æ¹ä¾¿æ§å¶å 容æ¾ç¤ºãæ¯å¦è¯´å¤çé³é¢åè§é¢ä¹é´çæ¶é´åæ¥ãéè¿setDisplayModeï¼ä½ å¯ä»¥è®¾ç½®ç¨äºæ¾ç¤ºçå¾å±ï¼å å建ä¸ä¸ª AVPlayerLayer 对象ä½ä¸ºæ¾ç¤ºå¾å±ï¼ç¶åä¿®æ¹å®çå±æ§ãé»è®¤æ¯AVLayerVideoGravityResizeAspect å±æ§ï¼å¦å¤ä¹å¯ä»¥è®¾ç½® AVLayerVideoGravityResizeAspectFill å±æ§åAVLayerVideoGravityResize å±æ§ã AVLayerVideoGravityResizeAspect ä¿æè§é¢ç宽é«æ¯å¹¶ä½¿ææ¾å 容èªå¨éåºææ¾çªå£ç大å°ãAVLayerVideoGravityResizeAspectFill ååè 类似ï¼ä½å®æ¯ä»¥ææ¾å 容填å èä¸æ¯éåºææ¾çªå£ç大å°ãæåä¸ä¸ªå¼ä¼æ伸ææ¾å 容以éåºææ¾çªå£ã
Player ç±»çå®æ´ä»£ç å¦ä¸ï¼
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class AVPlayer;
@interface Player : UIView
@property(nonatomic, strong) AVPlayer * player;
-(BOOL) setAirPlay:(BOOL) airplayMode;
@end
#import "Player.h"
#import <AVFoundation/AVFoundation.h>
@implementation Player
+ (Class)layerClass {
return [AVPlayerLayer class];
}
-(AVPlayer *) player{
return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer*)player {
[(AVPlayerLayer*)[self layer] setPlayer:player];
}
//Enable or disable AirPlay mode
-(BOOL)setAirPlay:(BOOL)airplayMode{
return self.player.allowsAirPlayVideo=airplayMode;
}
@end
å¨ç¨åºä¸ä½¿ç¨ Player ï¼UIViewåç±»ï¼
ä¸è¿°ä»£ç å æ¬äºPlayer ï¼AVPlayer ç UIView åç±»ï¼ç.hæ件å.m æ件ã
è¦å¨ä¸ä¸ª UIViewController ä¸ä½¿ç¨è¿ä¸ª AVPlayer è§å¾ï¼æå¼ Xcode çæ äºæ¿(storyboard,åææ¯å建项ç®æ¶ä½¿ç¨äºâuseStoryboardâé项ï¼ãéä¸UIView(ä¸æ¯ UIViewControllerï¼å¹¶å°å®ç类修æ¹ä¸º Playerãä½ å¯ä»¥å¨ Identity é¢æ¿ç customeclass å段è¾å ¥ï¼ä¹å¯ä»¥ä»å®çä¸æå表ä¸éæ©ã
æå¼ Assistant Editor ,为 UIView åç±» Player å建ä¸ä¸ª IBOutlet,ä» view ç¨å³é®æå°å³è¾¹ç头æ件ä¸å³å¯ãå¦ä¸å¾æ示ã
å建IBActions å IBOutlets è¿æ¥
å建ä¸ä¸ªå§æå° klViewController ãå¨ Player ä¸å³é®ï¼ä» IBOutlet æä¸ä¸ªè¿æ¥çº¿å° klViewController (dock ä¸çé»è²æ¹å) ï¼å¦ä¸å¾æ示ã
æ·»å å§æè¿æ¥ klViewController
å¨æ äºçä¸ï¼å ä¸ä¸ª Toolbar ãå¨ Toolbar æ¯å å ¥ä¸¤ä¸ªæé®ï¼åå«å°æ ç¾ææ¬è®¾ç½®ä¸º Play å Pauseãç¶åå ä¸ä¸ª Switchï¼ç¨äºåæ¢AirPlay å¼å ³ç¶æãä¸ºè¿ 3 个æ§ä»¶å建ç¸åºç IBActionï¼å¯ä»¥ç¨ Assistant Editorï¼ãå©ä¸çäºæ å¨ klViewController ç±»ä¸è¿è¡ã
æå¼ klViewController.h æ件ï¼å å ¥ @class Player å @class AVPlayer è¯å¥å¹¶å¯¼å ¥ AVFoundation æ¡æ¶å Player.hãæºä»£ç è§ä¸ï¼
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Player.h"
@class Player;
@class AVPlayer;
@interface klViewController : UIViewController
声æä¸ä¸ª AVPlayer å±æ§ï¼ç¨äºå è½½å° Palyer è§å¾ä¸ã
声æä¸ä¸ª NSURL å±æ§ãç¶åæ¯ Switch æ§ä»¶ç IBOutletï¼ç¨ Assistant Editorå建ï¼ã
kvLiewController.h æ件代ç å¦ä¸ï¼
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import Player.h
@class Player;
@class AVPlayer;
@interface klViewController : UIViewController
@property(nonatomic, strong) AVPlayer * myPlayer;
@property(nonatomic, strong) NSURL * avContentUrl;
@property (strong, nonatomic) IBOutlet Player *airPlayView;
@property (nonatomic, retain) IBOutlet UISwitch * AirPlaySwitch;
- (IBAction)PlayVideo:(id)sender;
- (IBAction)PauseVideo:(id)sender;
- (IBAction)isAirPlayOn:(id)sender;
@end
æ¥ä¸æ¥æ¯ç±»çå®ç°æ件ãå¨ viewDidLoad æ¹æ³ä¸ï¼ç¨ä¸ä¸ªè§é¢æ件ç URL å°ååå§å avContentUrlï¼
avContentUrl = [[NSURL alloc] initWithString
ç¶åç¨ avContentUrl åå§åä¸ä¸ª AVPlayer ï¼ç¨äºç»å¤´æ件ä¸å®ä¹ç myPlayer å±æ§èµå¼ãå° myPlayer èµç» airPlayView ç player å±æ§ï¼airPlayerView æ¯ä¸ä¸ª Player 对象ãå¦ä¸å代ç æ示ï¼
self.myPlayer = [AVPlayer playerWithURL:avContentUrl];
[airPlayView setPlayer:[self myPlayer]];
ç°å¨ï¼è¦å®ç°ä¸¤ä¸ªæé®çè§é¢ææ¾åæååè½ï¼ä»¥å Switch æ§ä»¶ç AirPlay å¼å ³åè½ãææ¾æé®ç action æ¹æ³ä»£ç å¦ä¸æ示ï¼
- (IBAction)PlayVideo:(id)sender {
[self.myPlayer play];
}
æåæé®ç action æ¹æ³ä»£ç å¦ä¸æ示ï¼
- (IBAction)PauseVideo:(id)sender {
[self.myPlayer pause];
}
Switch æ§ä»¶ç action æ¹æ³ä»£ç å¦ä¸æ示ï¼
- (IBAction)isAirPlayOn:(id)sender {
AirPlaySwitch = (UISwitch *) sender;
if (AirPlaySwitch.on) {
[airPlayView setAirPlay:NO];
}else
{
[airPlayView setAirPlay:YES];
}
}
ç»å°¾
å¦ä¸ä¸ªä¸ AirPlay æå ³çå±æ§æ¯ usesAirPlayVideoWhileAirPlayScreenIsActive ï¼å®ç¨äºèªå¨å¨å¨ææ¾æé´å° AVPlayer åæ¢å° AirPlay,å½ç¶ä» ä» æ¯å¨ AirPlay å·²å¼å¯çæ åµä¸ãé»è®¤æ¯ false çã
è¦è¿è¡æ¬ç¤ºä¾ç¨åºï¼éè¦å¨ iPad ä¸è¿è¡ï¼å¹¶å° iPad è¿æ¥è³ Apple TV åä¸ wifi ç½ç»ï¼ç¶åè§é¢èµæºç URL å¿ é¡»æ¯ææçãå¨æ¨¡æå¨ä¸AirPlay æ¯æ æçã
klViewController.h æ件
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Player.h"
@class Player;
@class AVPlayer;
@interface klViewController : UIViewController
@property(nonatomic, strong) AVPlayer * myPlayer;
@property(nonatomic, strong) NSURL * avContentUrl;
@property (strong, nonatomic) IBOutlet Player *airPlayView;
@property (nonatomic, retain) IBOutlet UISwitch * AirPlaySwitch;
- (IBAction)PlayVideo:(id)sender;
- (IBAction)PauseVideo:(id)sender;
- (IBAction)isAirPlayOn:(id)sender;
@end
klViewController.m æ件
#import "klViewController.h"
@implementation klViewController
@synthesize airPlayView;
@synthesize avContentUrl, myPlayer, AirPlaySwitch;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//This is an Apple sample video
avContentUrl = [[NSURL alloc] initWithString:@" self.myPlayer = [AVPlayer playerWithURL:avContentUrl];
[airPlayView setPlayer:[self myPlayer]];
[self.myPlayer play];
}
- (void)viewDidUnload
{
[self setAirPlayView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)PlayVideo:(id)sender {
[self.myPlayer play];
}
- (IBAction)PauseVideo:(id)sender {
[self.myPlayer pause];
}
- (IBAction)isAirPlayOn:(id)sender {
AirPlaySwitch = (UISwitch *) sender;
if (AirPlaySwitch.on) {
[airPlayView setAirPlay:NO];
}else
{
[airPlayView setAirPlay:YES];
}
}
@end