The opening

I haven’t written Jane’s book for a long time, because I ran to learn live broadcast out of curiosity, today I will share my feelings.




3033

Now live or a hot technology, Jane books, git on a few article reading quantity and value are pretty good, but others, after all, is others, sometimes even according to write it again, will also have their own progress, especially if you according to your own thoughts and ideas, on the basis of study the code in their own way to create it, Will find a lot of things look to understand but with their real hands to achieve it is one thing, feeling quite deep, so have this brief book. Project is based on ijkPlayer here first put the project open source MyShow(need to import their own IJK), like to give a star oh.

rendering




Figure display page




Live figure

Project framework




PushStremViewController_h.png

There are dependency libraries, third parties, and some tools. The main code is divided into three categories: Live is for Live content, and PushStrem is for pushing streams.

What I learned during the live broadcast

In the live broadcast, the interface used is the interface provided on the Internet. After the data is displayed on the TableView, the display effect is shown in the display page diagram, including the number of viewers, name and address of anchor, head picture of anchor, large picture of anchor, etc. The content is determined by the data returned from the background. Click the cell, pass in the corresponding picture, and make the transition, as shown below.




Transitions figure

Feel the ferry, and appeared in the process of playing CARDS, or broken network, the refresh effect is an important factor of the whole experience are in good condition, so here I hope you are doing when carefully have a look at how to achieve, as well as to refresh the encapsulation, although are relatively common, but we are influenced by intuitive visual experience. Is actually a GIF playback method. Found an online method or very good use.

- (void)showGifLoding:(NSArray *)images inView:(UIView *)view {if (! images.count) { images = @[[UIImage imageNamed:@"hold1_60x72"], [UIImage imageNamed:@"hold2_60x72"], [UIImage imageNamed:@"hold3_60x72"]]; } UIImageView *gifView = [[UIImageView alloc] init]; if (! view) { view = self.view; } [view addSubview:gifView]; [gifView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(@0); make.width.equalTo(@60);  make.height.equalTo(@70); }]; self.gifView = gifView; [gifView playGifAnim:images]; } // Cancel GIF loading animation - (void)hideGufLoding {[self.gifView stopGifAnim]; self.gifView = nil; }Copy the code
  • Real-time reminders of the network during live broadcast. Changes of the network may have a great impact on the effect of live broadcast, so it is necessary to monitor network changes. Or you can do it another way.
/ / 1. Get the manager network monitoring AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager]; / / 2. Set up the network state changes after treatment [manager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {/ / when the network state has changed, Is called the block switch (status) {case AFNetworkReachabilityStatusUnknown: {/ / unknown network [MBProgressHUD showSuccess: @ "unknown network"]; break;} case AFNetworkReachabilityStatusNotReachable: / / not network (offline) {[MBProgressHUD showSuccess: @ "no Internet connection"]; break;} case AFNetworkReachabilityStatusReachableViaWWAN: {/ / cell phones bring network [MBProgressHUD showSuccess: @ "4 g / 3 g traffic state"]; break;} case AFNetworkReachabilityStatusReachableViaWiFi: // MBProgressHUD showSuccess {[MBProgressHUD showSuccess:@" "]; break;}}}]; // 3. StartMonitoring [manager startMonitoring];Copy the code

Barrage is essentially the rendering of multiple sprites in time. There are already mature solutions on the PC/Web; The most famous one on Android is BiliBili’s open source DanmakuFlameMaster. The wheel we use today is called BarrageRenderer, which saves the difficulty of making bullets by ourselves and saves time.

  _renderer = [[BarrageRenderer alloc] init];Copy the code

After setting, we set the number and production method of the barrage. The open source PLIST file is used here, so the barrage is also the content read in the PLIST file. In the actual project, we can analyze the data returned from the background, and we can also input the barrage to display.

- (void)autoSendBarrage { NSInteger spriteNumber = [_renderer spritesNumberWithName:nil]; If (spriteNumber <= 50) {// Limit the number of bullets on the screen [_renderer receive:[self walkTextSpriteDescriptorWithDirection:BarrageWalkDirectionR2L]]; }} #pragma mark - long _index = 0; / / / generate elf description - cut text barrage - (BarrageDescriptor *) walkTextSpriteDescriptorWithDirection direction: (NSInteger) { BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init]; descriptor.spriteName = NSStringFromClass([BarrageWalkTextSprite class]); descriptor.params[@"text"] = self.danMuText[arc4random_uniform((uint32_t)self.danMuText.count)]; descriptor.params[@"textColor"] = Color(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256)); descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50); descriptor.params[@"direction"] = @(direction); Descriptor. Params [@"clickAction"] = ^{UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@" prompt "message:@" reload clicked" Delegate :nil cancelButtonTitle:@" cancel "otherButtonTitles:nil]; [alertView show]; }; return descriptor; } - (NSArray *)danMuText { return [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"danmu.plist"  ofType:nil]]; }Copy the code
  • The realization of the push stream is indispensable in live streaming. Compared with the realization of a push stream by ourselves, it is simpler to use the known framework. Here, we use the open source framework LFLiveKit of Youku, which makes our development more convenient. We can build a server on the computer side to test, specific method put two portals, I also learn from here thanks to open source, quick integration of iOS based on RTMP video push stream,HLS- build Nginx streaming server reference to the above tutorial, we successfully achieve the purpose of push stream.

    Sigh with emotion

    A small amount of code may help some modules, but it is also important to integrate the code into our projects to accomplish the logic we want to implement ourselves.

    In the pit of

    When we push the stream according to the method of everyone on Github, the simulator runs without any problem, but the real machine is connected, and suddenly confused, what happened? Bugs that appear in this form are usually the ones that look spooky. At first we suspected that there was a lack of third-party libraries, then we checked if there were any, then we relied on libraries, then we found out that there were no, and finally we had someone to suspect.




    MyShow_xcodeproj.png

    Through careful reading, I found a suspicious word, ENABLE_BITCODE. Yes, it is ENABLE_BITCODE. Let’s solve it.




    MyShow_xcodeproj.png

    So we can run a real test later and find no problems. Problem solved. Originally integrating LFLiveKit required turning off Bitcode.

    Thank you

    Here or thanks to a wave of open source code and a brief book to explain, really benefits bandit shallow. Thanks to JIAAIR, the author of Jane’s book, Monkey_ALin, and many programmers who provide principles and code knowledge on the Internet, I was able to complete my live broadcast project by referring to the ideas of various gods from a daze. Draw on a lot of knowledge and learn a lot. Thanks again.

    Afterword.

    Because it is the first time to make, just achieve the function of part, here basically is just to give you a reference code, or that sentence, although there are a lot of live broadcast program code, also have their respective reference each other, but we need to learn, the key is how do we go a little change according to oneself idea and implement it, specific knowledge of theory and logic, I will study it in the following theoretical chapters and then share it with you. Please give me a star if you like. Code at the same time put VLC player, I hope to help you. Reprint with indication of source and address.

    Address of the project, remember star