IJKMediaFramework析恢、LFLiveKit實現(xiàn)視頻直播

視頻直播

<h5>拉流:</h5>

從服務(wù)器獲取視頻直播流地址给梅,播放直播 使用IJKMediaFramework.framework
使用:

    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    [options setPlayerOptionIntValue:1  forKey:@"videotoolbox"];
    // 幀速率(fps) (可以改,確認非標準楨率會導致音畫不同步钾虐,所以只能設(shè)定為15或者29.97)
    [options setPlayerOptionIntValue:29.97 forKey:@"r"];
    // -vol——設(shè)置音量大小,256為標準音量。(要設(shè)置成兩倍音量時則輸入512啰脚,依此類推
    [options setPlayerOptionIntValue:512 forKey:@"vol"];
    IJKFFMoviePlayerController *moviePlayer = [[IJKFFMoviePlayerController alloc] initWithContentURLString:flv withOptions:options];
    moviePlayer.view.frame = self.contentView.bounds;
    // 填充fill
    moviePlayer.scalingMode = IJKMPMovieScalingModeAspectFill;
    // 設(shè)置自動播放(必須設(shè)置為NO, 防止自動播放, 才能更好的控制直播的狀態(tài))
    moviePlayer.shouldAutoplay = NO;
    // 默認不顯示
    moviePlayer.shouldShowHudView = NO;
    
    [self.contentView insertSubview:moviePlayer.view atIndex:0];
    
    [moviePlayer prepareToPlay];
    
    self.moviePlayer = moviePlayer;

<h5>推流:</h5>

開始直播,視頻采集和視頻流處理與傳輸
直播采用L
LFLiveKit 支持的流類型
rtmp格式 实夹、 tcp 傳輸flv格式

<h5>使用</h5>
開播邏輯

- (void)startLive {
    MPLiveModel *liveModel = [ZQGlobalObject sharedZQGlobalObject].liveModel;
    // 拼接推流地址 “url/流名”
    NSString *url = [NSString stringWithFormat:@"%@/%@", liveModel.rtmpUrl, liveModel.videoId];
    // 創(chuàng)建流對象
    LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
    stream.url = url;
    // 推流對象開始推流
    [_session startLive:stream];
}

進出后臺處理

#pragma mark 應(yīng)用程序通知監(jiān)聽
- (void)applicationNotification:(NSNotification *)note {
    if (_session) {
        // 進入后臺
        if (note.name == UIApplicationDidEnterBackgroundNotification) { // 進入后臺
            NSLog(@"進入后臺");
            [_controlView pauseLive];
        // 從后臺回到直播間
        } else if (note.name == UIApplicationWillEnterForegroundNotification) {
            if (!_session.running) {
                _session.running = YES;
            }
            [self startLive];
        }
    }
}

退出直播間橄浓,釋放對象

- (void)dealloc {
    if (_session) {
        _session.delegate = nil;
        _session = nil;
    }
    NSLog(@"退出直播間");
}

創(chuàng)建推流對象并設(shè)置播放參數(shù)

- (LFLiveSession *)session {
    if(!_session){
       
        _session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Medium2 orientation:UIInterfaceOrientationPortrait] liveType:LFLiveRTMP];
        _session.delegate = self;
        _session.running = YES;
        _session.preView = _preview;
    }
    return _session;
}

創(chuàng)建推流要展示推流效果的視圖對象

- (MPLiveControlView *)controlView {
    if (_controlView == nil) {
        _controlView = [[MPLiveControlView alloc] initWithFrame:self.view.frame];
        _controlView.delegate = self;
        _controlView.session = self.session;
    }
    return _controlView;
}

推流狀態(tài)的回調(diào)

- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state{
    NSLog(@"liveStateDidChange: %ld", state);
    WS(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        switch (state) {
            case LFLiveReady:
                [weakSelf.controlView setLiveState:MPLiveStateLoading];
                break;
            case LFLivePending:
                [weakSelf.controlView setLiveState:MPLiveStateLoading];
                break;
            case LFLiveStart:
                [weakSelf.controlView setLiveState:MPLiveStatePlay];
                break;
            case LFLiveError:
    //            _stateLabel.text = @"連接錯誤";
                break;
            case LFLiveStop:
                [weakSelf.controlView setLiveState:MPLiveStateStop];
                break;
            default:
                break;
        }
    });
}
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug*)debugInfo{
    NSLog(@"debugInfo: %lf", debugInfo.dataFlow);
}
- (void)liveSession:(nullable LFLiveSession*)session errorCode:(LFLiveSocketErrorCode)errorCode{
    NSLog(@"errorCode: %ld", errorCode);
}

彈幕可以使用BarrageRenderer

簡單使用:

_renderer = [[BarrageRenderer alloc] init];
// 設(shè)置彈幕的顯示區(qū)域. 基于父控件的.
_renderer.canvasMargin = UIEdgeInsetsMake(ALinScreenHeight * 0.3, 10, 10, 10);
[self.contentView addSubview:_renderer.view];

彈幕配置

#pragma mark - 彈幕描述符生產(chǎn)方法
/// 生成精靈描述 - 過場文字彈幕
- (BarrageDescriptor *)walkTextSpriteDescriptorWithDirection:(NSInteger)direction
{
    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:@"提示" message:@"彈幕被點擊" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alertView show];
    };
    return descriptor;
}
 
[_renderer start];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市亮航,隨后出現(xiàn)的幾起案子荸实,更是在濱河造成了極大的恐慌,老刑警劉巖缴淋,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件准给,死亡現(xiàn)場離奇詭異,居然都是意外死亡重抖,警方通過查閱死者的電腦和手機露氮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钟沛,“玉大人畔规,你說我怎么就攤上這事『尥常” “怎么了叁扫?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長延欠。 經(jīng)常有香客問我陌兑,道長,這世上最難降的妖魔是什么由捎? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任兔综,我火速辦了婚禮,結(jié)果婚禮上狞玛,老公的妹妹穿的比我還像新娘软驰。我一直安慰自己,他們只是感情好心肪,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布锭亏。 她就那樣靜靜地躺著,像睡著了一般硬鞍。 火紅的嫁衣襯著肌膚如雪慧瘤。 梳的紋絲不亂的頭發(fā)上戴已,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音锅减,去河邊找鬼糖儡。 笑死,一個胖子當著我的面吹牛怔匣,可吹牛的內(nèi)容都是我干的握联。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼每瞒,長吁一口氣:“原來是場噩夢啊……” “哼金闽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起剿骨,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤代芜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后懦砂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜒犯,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡组橄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年荞膘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玉工。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡羽资,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出遵班,到底是詐尸還是另有隱情屠升,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布狭郑,位于F島的核電站腹暖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏翰萨。R本人自食惡果不足惜脏答,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亩鬼。 院中可真熱鬧殖告,春花似錦、人聲如沸雳锋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽玷过。三九已至爽丹,卻和暖如春筑煮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背粤蝎。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工咆瘟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人诽里。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓袒餐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谤狡。 傳聞我的和親對象是個殘疾皇子灸眼,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容