IOS Live (ii)

Source: Internet
Author: User

Push-Flow is a third-party iflivekit framework. This framework is based on RTMP protocol, the C language and OC language is very clear, very friendly to OC language, relatively simple to use.

The gpuiimage is integrated inside the Iflivekit. The interior achieves such glamorous effects as image rendering. Reduces the development time of the glamorous effect of debugging.

Requirements: Create a simple push-flow page. Included features are: 1. Push-flow status monitoring. 2. Switch the camera. 3. Toggle the glamorous effect. 4. Switch push-flow.

One. Before pushing the stream, you need to check the camera and microphone for permission to open and start the camera, the core code is as follows:

//Determine if there is a camera if (![ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {[Self showInfo:@]        Your device does not have a webcam or related driver and cannot be streamed live "];    Return }//To determine if there is a camera permission avauthorizationstatus authorizationstatus = [Avcapturedevice authorizationstatusformediatype:avmedia    Typevideo];        if (authorizationstatus = = avauthorizationstatusrestricted| | authorizationstatus = = avauthorizationstatusdenied) { The [self showinfo:@] app needs access to your webcam.        \ n Please enable webcam-settings/privacy/webcam "];    return;    } avaudiosession *audiosession = [Avaudiosession sharedinstance]; if ([Audiosession respondstoselector: @selector (requestrecordpermission:)]) {[Audiosession performselector: @selecto            R (requestrecordpermission:) withobject:^ (BOOL granted) {if (granted) {return YES; The else {[self showinfo:@] app needs to access your microphone.                \ n Please enable microphone-settings/privacy/microphone "];            return NO;    }        }]; }

Two. Create a button. Click the start stream code below:

-(lflivesession*) session{if (!_session) {/***   default resolution 368 * 640 Audio: 44.1 iphone6 above 48 dual channel direction vertical screen ***/ _session = [[Lflivesession alloc] initwithaudioconfiguration:[lfliveaudioconfiguration defaultconfiguration]                Videoconfiguration:[lflivevideoconfiguration Defaultconfigurationforquality:lflivevideoquality_medium2]]; /**    own custom high quality audio 128K resolution set to 720*1280 direction vertical screen *//////Lfliveaudioconfiguration *audioconfiguration = [LF         Liveaudioconfiguration new];         Audioconfiguration.numberofchannels = 2;         Audioconfiguration.audiobitrate = lfliveaudiobitrate_128kbps;                  Audioconfiguration.audiosamplerate = lfliveaudiosamplerate_44100hz;         Lflivevideoconfiguration *videoconfiguration = [lflivevideoconfiguration new];         Videoconfiguration.videosize = Cgsizemake (720, 1280);         Videoconfiguration.videobitrate = 800*1024;         Videoconfiguration.videomaxbitrate = 1000*1024; Videoconfiguration.videominbitrate = 500*1024;         Videoconfiguration.videoframerate = 15;         Videoconfiguration.videomaxkeyframeinterval = 30;         Videoconfiguration.orientation = uiinterfaceorientationportrait;                  Videoconfiguration.sessionpreset = lfcapturesessionpreset720x1280; _session = [[Lflivesession alloc] initwithaudioconfiguration:audioconfiguration videoconfiguration:         Videoconfiguration Livetype:lflivertmp];        *//Set proxy _session.delegate = self;        _session.running = YES;    _session.preview = Self.livingpreview; } return _session;}    -(Ibaction) starttouched: (ID) Sender {lflivestreaminfo *stream = [Lflivestreaminfo new];    Local push stream Address Stream.url = @ "Rtmp://192.168.199.131:1935/rtmplive/room";    Self.rtmpurl = Stream.url; [Self.session Startlive:stream];}

3. Create a button click to close the push stream, the code is as follows

-(Ibaction) endtouched: (ID) Sender {    //End live broadcast    [Self.session stoplive];    Self.stateLable.text = [NSString stringwithformat:@ Status: Live is closed \nrtmp:%@ ", Self.rtmpurl];}

4. Create a button click Switch front and rear camera, code as follows

-(Ibaction) camabtntouched: (ID) Sender {    Avcapturedeviceposition Devicepositon = self.session.captureDevicePosition;    Self.session.captureDevicePosition = (Devicepositon = = avcapturedevicepositionback)? Avcapturedevicepositionfront:avcapturedevicepositionback;    NSLog (@ "Toggle Front/rear Camera");}

5. Create a button to set the glamorous function, the code is as follows

-(Ibaction) beautiybtntouched: (ID) Sender {(    (uibutton*) sender). Selected =! ( (uibutton*) sender). Selected;    The default is to turn on the beauty function    self.session.beautyFace =!self.session.beautyface;}

6. Push Stream status monitor, accept Agent, code as follows:

#pragma mark-lfstreamingsessiondelegate/** live status changed would callback */-(void) Livesession: (Nullable lfliveses    Sion *) session Livestatedidchange: (lflivestate) state{nsstring *tempstatus;            Switch (state) {Case lfliveready:tempstatus = @ "in preparation";        Break            Case lflivepending:tempstatus = @ "Connected";        Break            Case lflivestart:tempstatus = @ "Connected";        Break            Case lflivestop:tempstatus = @ "Disconnected";        Break            Case lfliveerror:tempstatus = @ "Connection error";        Break    Default:break; } self.stateLable.text = [nsstring stringwithformat:@ Status:%@\nrtmp:%@ ", Tempstatus, Self.rtmpurl];} /** Live Debug Info callback */-(void) Livesession: (Nullable lflivesession *) session Debuginfo: (Nullable lflivedebug*) debuginfo{}/** Callback socket errorcode */-(void) Livesession: (Nullable lflivesession*) session ErrorCode: (Lflivesock  Eterrorcode) errorcode{  } 

6. You also need to see your own push-flow screen, and observe the glamorous effect, the code is as follows:

-(UIView *) livingpreview{    if (!_livingpreview) {        UIView *livingpreview = [[UIView alloc] initWithFrame: Self.view.bounds];        Livingpreview.backgroundcolor = [Uicolor clearcolor];        Livingpreview.autoresizingmask = Uiviewautoresizingflexiblewidth | Uiviewautoresizingflexibleheight;        [Self.view Insertsubview:livingpreview atindex:0];        _livingpreview = Livingpreview;    }    return _livingpreview;}

At this point, the push-flow work is completed. Over here

IOS Live (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.