After the push flow is complete, you need to pull the stream to complete the live broadcast. Pull stream We are also using an open source third-party library, Ijkmediaframework.
This library is essentially a player that can play the FLV format of the player. It's similar to the Avplayer that iOS comes with.
Implementation features: 1. Pull stream playback. 2. Monitoring.
I. Create a bitmap and lag-placeholder, code as follows:
Pre-broadcast placeholder images
-(Uiimageview *) placeholderview{ if (!_placeholderview) { _placeholderview = [[Uiimageview alloc] init]; _placeholderview.frame = self.view.bounds; _placeholderview.image = [UIImage imagenamed:@ "profile_user_414x414"]; Forced layout [_placeholderview layoutifneeded]; } return _placeholderview;}
Lag action-(void) showactivityview{ if (!_activity) { _activity= [[Uiactivityindicatorview alloc] Initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; _activity.frame = CGRectMake ((scream_weight-100) *0.5, (scream_hight-100) *0.5, [+]; } [Self.activity startanimating]; [Self.view addSubview:self.activity];} Close the lag-placeholder action-(void) stopactivityview{ if ([_activity isanimating]) { [_activity startanimating]; } [_activity Removefromsuperview]; _activity = nil;}
Two. Pull stream playback (create player playback), the code is as follows:
-(void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.placeHolderView]; [Self showactivityview]; Ijkffoptions *options = [Ijkffoptions Optionsbydefault]; [Options Setplayeroptionintvalue:1 forkey:@ "Videotoolbox"]; Frame rate (fps) (can be changed to confirm that non-standard frames will cause the tone to be out of sync, so it can only be set to 15 or 29.97) [options setplayeroptionintvalue:29.97 forkey:@ "R"]; -vol--set the volume size, 256 is the standard volume. (Enter 512 when you want to set the volume to twice times, and so on [Options setplayeroptionintvalue:512 forkey:@ "Vol"]; Ijkffmovieplayercontroller *movieplayer = [[Ijkffmovieplayercontroller alloc] Initwithcontenturlstring:play_url Withoptions:options]; MoviePlayer.view.frame = Self.view.bounds; Movieplayer.scalingmode = Ijkmpmoviescalingmodeaspectfill; Set AutoPlay (must be set to NO, prevent AutoPlay, to better control the status of the live) Movieplayer.shouldautoplay = no; The default does not show Movieplayer.shouldshowhudview = no; [Self.view InsertSubview:moviePlayer.view atindex:0]; [MoviePlayer Preparetoplay]; Self.movieplayer = MoviePlayer; Setting up Monitoring [Self addobserver]; [Self.view addSubview:self.outBtn];}
Three. Set the listener (mainly the listening cache), the code is as follows:
-(void) addobserver{//Monitor load status change notification [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (loadstat Edidchange:) name:ijkmpmovieplayerloadstatedidchangenotification Object:self.moviePlayer];} -(void) Loadstatedidchange: (nsnotification *) notification{//status is buffered almost complete, can be played continuously if (Self.moviePlayer.loadState & Ijkmpmovieloadstateplaythroughok)! = 0) {if (!self.movieplayer.isplaying) {//start playing [Self.mo Vieplayer Play]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1 * nsec_per_sec)), Dispatch_get_main_queue (), ^{ if (_placeholderview) {[_placeholderview Removefromsuperview]; _placeholderview = nil; } [self Stopactivityview]; }); }else{//If the network status is not good, the recovery after disconnection, also need to remove the load if ([_activity isanimating]) {dispatch_after (dis Patch_time (Dispatch_time_now, (int64_t) (1 * nsec_per_sec)), DispaTch_get_main_queue (), ^{[self stopactivityview]; }); }}}//buffer in else if (Self.moviePlayer.loadState & ijkmpmovieloadstatestalled) {[Self showactivi Tyview]; /* Here the host may have ended the live broadcast. We need to ask the server to see if the host has finished broadcasting. Method: 1, from the server to obtain whether the host has been closed live. Advantages: To be able to correctly get the host side is broadcasting. Disadvantage: Host side abnormal crash case is no way to notify the server that the live shutdown. 2, the user HTTP request this address, if the request successfully indicates that the live broadcast is not finished, otherwise the end advantage: can be real to get the host side has the push stream data disadvantage: If the host side packet loss rate is too low, but can recover the situation, the data request is also failed. */__weak typeof (self) weakself = self; [[Alinnetworktool Sharetool] Get:play_url parameters:nil progress:nil success:^ (nsurlsessiondatatask * _Nonnull task, ID _nullable responseobject) {NSLog (@ "Request succeeds%@, wait for playback to continue", responseobject); } failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {NSLog (@ "request failed, load failed interface, close player%@", Error); [Weakself performselector: @selector(outbtntouched)]; }]; }}
Four. Remember to release before closing
-(void) dealloc{ if (_movieplayer) { [_movieplayer shutdown]; [_movieplayer.view Removefromsuperview]; _movieplayer = nil; } [[Nsnotificationcenter defaultcenter]removeobserver:self];}
Summary: There are many defects in the project waiting for one by one to be perfected. Over here
IOS Live (iii)