推流部分

2017-12-19  本文已影响20人  SuperMan_Wang

这部分呢主要总结下,如何把移动端的摄像头和麦克风采集到的内容,利用rtmp协议发送到我们之前搭好的服务端。
没有搭建服务端的请看我之前的简书搭建服务端
好的,下面进入正题。

iOS端进行推流的话我用的是 LFLiveKit

很好用,可以直接用CocoaPods 加上

pod 'LFLiveKit'

就可以用了。

在controller里边

#import "LFLiveKit/LFLiveKit.h"

然后,

#define PUT_Strean @"rtmp://192.168.1.109:1935/zbcs/room"//推流URL

接着,

@property (nonatomic, strong) LFLiveSession *session;

@property (nonatomic, strong) UIView *pp;

- (LFLiveSession*)session {
    if (!_session) {
        _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
        _session.preView = self.pp;
        _session.running = YES;
        _session.showDebugInfo = YES;
        _session.captureDevicePosition = AVCaptureDevicePositionBack;
        _session.delegate = self;
    }
    return _session;
}

- (void)startLive {
    LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
    streamInfo.url = [[NSUserDefaults standardUserDefaults] objectForKey:@"putURL"];
    [self.session startLive:streamInfo];
}

- (void)stopLive {
    [self.session stopLive];
}
//LFLiveSessionDelegate部分
//MARK: - CallBack:
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange: (LFLiveState)state {
    
    switch (state) {
        case LFLiveReady:   NSLog(@"准备"); break;
        case LFLivePending: NSLog(@"连接中"); break;
        case LFLiveStart:   NSLog(@"已连接"); break;
        case LFLiveStop:
            self.button.selected = NO;
            self.button.backgroundColor = [UIColor blueColor];
            NSLog(@"已断开"); break;
        case LFLiveError:
            self.button.selected = NO;
            self.button.backgroundColor = [UIColor blueColor];
            NSLog(@"出错"); break;
        case LFLiveRefresh: NSLog(@"正在刷新"); break;
            
        default: NSLog(@"连接状态改变。"); break;
    }
}

- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable  LFLiveDebug*)debugInfo {
    
    NSLog(@"debug -- %@", debugInfo);
}

- (void)liveSession:(nullable LFLiveSession*)session errorCode:(LFLiveSocketErrorCode)errorCode {
    
    NSLog(@"连接失败 -- %lu", (unsigned long)errorCode);
    self.button.selected = NO;
    self.button.backgroundColor = [UIColor blueColor];
}

最后,在你需要开始直播的地方调用开始直播的方法。需要停止直播的地方调用停止直播的方法就可以了。

上一篇 下一篇

猜你喜欢

热点阅读