iOS多播放器封裝

來源:互聯網
上載者:User

標籤:cimage   不同的   top   edit   切換   nbsp   對象   integer   play   

今年在做直播業務的時候遇到一些問題,就是在一個套播放器UI中需要多種不同的播放器(AVPlayer、IJKPlayer、AliPlayer)支援,根據ABTest開關來切換具體使用哪種播放器,並且還要對播放器進行日誌統計。
首先可以想到的是需要對不同的播放器封裝一個統一的介面,對於UI來講,不需要關係當前操控的是哪個播放器。


多播放器架構.jpg

其中player protocal是個關鍵

@protocol VideoPlaybackProtocal <NSObject>@property (nonatomic, assign, setter=setDelegate:) id<LivePlaybackEventListener> delegate;-(void)addPlaybackEventListener:(id<LivePlaybackEventListener>)listener;-(void)removePlaybackEventListener:(id<LivePlaybackEventListener>)listener;@property (nonatomic, strong, readonly) UIView * view;@property (nonatomic, assign, readonly) CMTime currentPlaybackTime;@property (nonatomic, assign, readonly) CMTime duration;@property (nonatomic, assign, readonly) NSURL *currentPlayUrl;@property (nonatomic, assign, readonly) BOOL supportsRTMP;- (void)stopDueToLiveDidEnd;- (void)resume;- (void)pause;- (void)replay;- (void)reload;- (void)startPlayingWithPlayInfo:(NSURL*)url;- (void)shutdown;- (void)seekToProgress:(double)progress event:(PlayerEvent)event;- (BOOL)isPlaying;@end

 

然後三個播放器對這個介面進行各自的實現。這樣對於ViewController來說播放器就是一個id<VideoPlaybackProtocal>,對播放器進行暫停播放各種操作就可以。打算用哪個播放器,就把id<VideoPlaybackProtocal>執行個體化成哪個播放器的實現,輕鬆又簡單。

其中這裡的Listener是一個delegate,當播放器出現播放完成、播放錯誤等事件時,Listener會對註冊了Listener的對象進行廣播,目前需要註冊的是ViewController和Logger,UI收到廣播來進行圖形介面的變化,Logger則負責將事件統計上傳到Log Service器當中。這個廣播器通過一個NSHashTable * _listeners;來保證註冊了廣播的observe為弱引用,然後通過遍曆observe來進行delegate調用,例如:

- (void)videoPlayerController:(id< VideoPlaybackProtocal >)playerController didFailWithError:(NSError *)error playerLog:(NSDictionary *)playerLog{    [self _enumerateListeners:^(id listener, NSUInteger idx) {        if ([listener respondsToSelector:_cmd]) {            [listener videoPlayerController:playerController didFailWithError:error playerLog:playerLog];        }    }];}

 

 

 

iOS多播放器封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.