This year in the live broadcast business encountered some problems, that is, in a set of player UI needs a number of different players (Avplayer, Ijkplayer, Aliplayer) support, according to the abtest switch to switch the specific use of which player, and also to the player log statistics.
The first thing you can think of is the need to encapsulate a unified interface for different players, and for the UI, there is no need to have a relationship with which player is currently manipulated.
Multi-player architecture. jpg
Where player protocal is a key
@protocolVideoplaybackprotocal <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) ProgressEvent:(playerevent)Event;-(BOOL) isplaying;@end
The three players then perform their respective implementations of the interface. This way, for Viewcontroller, the player is a id<videoplaybackprotocal>, and the player can pause and play various actions. Which player is intended to use, it is easy and simple to instantiate the id<videoplaybackprotocal> into which player is implemented.
Where the listener is a delegate, when the player appears to play, playback errors and other events, listener will be registered listener objects broadcast, currently need to register viewcontroller and logger, The UI receives a broadcast to make changes to the graphical interface, and logger is responsible for uploading the event statistics to the log server. This broadcaster uses a observe to ensure that the NSHashTable * _listeners; broadcast is registered as a weak reference, and then iterates through the observe to make delegate calls, for example:
-(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 Multi player Package