iOS开发集成Bilibili/ijkplayer做视频直播

由于2016年直播行业特别火,也越来越多的人想做直播。可是视频直播一些传输协议(RTMP基于HTTP协议),视频采用什么H.264压缩,音频采用ACC等等这些太复杂了。所以我们需要集成第三方,下面我就为大家详细讲解集成步骤,不足之处欢迎交流 。        QQ1725865030 

1.框架名字: Bilibili/ijkplayer

注意事项:把Bilibili/ijkplayer下载下来你会发现这个文件很小,打开iOS里面的IJKMediaDemo也报错找不到#include "libavformat/avformat.h"这个文件。这个时候我们要在终端下载Bilibili/ijkplayer,具体步骤GitHub里面有说明,因为文件比较大就不一一讲解了。(弄不好的朋友可以加上方的@QQ我会把源码也给你,源码里面的步骤我标记的比较清晰)

2.接下来就是添加依赖的库了


3.接下来我们就要拷贝一些我们需要的代码了(这里我们需要简单的直播代码我已经拷贝下来了你直接创建一个继承于ViewController控制器就行了,把下面这些代码全部粘贴到你的.m文件里)

#import "DKPlayerViewController.h"//第1步#import@interface DKPlayerViewController ()//第1.1步@property(atomic, retain) idplayer;

@end

@implementation DKPlayerViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.navigationController.navigationBarHidden = YES;

[self initPlayer];

}

//第1.2步

- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

self.navigationController.navigationBarHidden = YES;

//注册直播需要用的通知

[self installMovieNotificationObservers];

//准备播放

[self.player prepareToPlay];

}

//第1.3步

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

//关闭直播

[self.player shutdown];

//移除

[self removeMovieNotificationObservers];

}

#pragma mark Install Movie Notifications

//1.4

/* Register observers for the various movie object notifications. */

-(void)installMovieNotificationObservers

{

//监听网络环境,监听缓冲方法

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(loadStateDidChange:)

name:IJKMPMoviePlayerLoadStateDidChangeNotification

object:_player];

//监听直播完成回调

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlayBackDidFinish:)

name:IJKMPMoviePlayerPlaybackDidFinishNotification

object:_player];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(mediaIsPreparedToPlayDidChange:)

name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification

object:_player];

//监听用户主动操作

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlayBackStateDidChange:)

name:IJKMPMoviePlayerPlaybackStateDidChangeNotification

object:_player];

}

#pragma mark Remove Movie Notification Handlers

//1.5

/* Remove the movie notification observers from the movie object. */

-(void)removeMovieNotificationObservers

{

[[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerLoadStateDidChangeNotification object:_player];

[[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerPlaybackDidFinishNotification object:_player];

[[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification object:_player];

[[NSNotificationCenter defaultCenter]removeObserver:self name:IJKMPMoviePlayerPlaybackStateDidChangeNotification object:_player];

}

//1.6

- (void)loadStateDidChange:(NSNotification*)notification

{

//    MPMovieLoadStateUnknown        = 0,未知

//    MPMovieLoadStatePlayable      = 1 << 0,缓冲结束可以播放

//    MPMovieLoadStatePlaythroughOK  = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES 缓冲结束自动播放

//    MPMovieLoadStateStalled        = 1 << 2, // Playback will be automatically paused in this state, if started

//暂停

IJKMPMovieLoadState loadState = _player.loadState;

if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {

NSLog(@"loadStateDidChange: IJKMPMovieLoadStatePlaythroughOK: %d\n", (int)loadState);

} else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {

NSLog(@"loadStateDidChange: IJKMPMovieLoadStateStalled: %d\n", (int)loadState);

} else {

NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState);

}

//    self.blurImageView.hidden = YES;

//    [self.blurImageView removeFromSuperview];

}

//1.7

- (void)moviePlayBackDidFinish:(NSNotification*)notification

{

//    MPMovieFinishReasonPlaybackEnded, 直播结束

//    MPMovieFinishReasonPlaybackError, 直播错误

//    MPMovieFinishReasonUserExited  用户退出

int reason = [[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];

switch (reason)

{

case IJKMPMovieFinishReasonPlaybackEnded:

NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason);

break;

case IJKMPMovieFinishReasonUserExited:

NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason);

break;

case IJKMPMovieFinishReasonPlaybackError:

NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason);

break;

default:

NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);

break;

}

}

//1.8

- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification

{

NSLog(@"mediaIsPreparedToPlayDidChange\n");

}

- (void)moviePlayBackStateDidChange:(NSNotification*)notification

{

//    MPMoviePlaybackStateStopped,

//    MPMoviePlaybackStatePlaying,

//    MPMoviePlaybackStatePaused,

//    MPMoviePlaybackStateInterrupted,

//    MPMoviePlaybackStateSeekingForward,

//    MPMoviePlaybackStateSeekingBackward

switch (_player.playbackState)

{

case IJKMPMoviePlaybackStateStopped: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState);

break;

}

case IJKMPMoviePlaybackStatePlaying: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState);

break;

}

case IJKMPMoviePlaybackStatePaused: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState);

break;

}

case IJKMPMoviePlaybackStateInterrupted: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState);

break;

}

case IJKMPMoviePlaybackStateSeekingForward:

case IJKMPMoviePlaybackStateSeekingBackward: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState);

break;

}

default: {

NSLog(@"IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState);

break;

}

}

}

//1.9设置播放的player

- (void)initPlayer {

IJKFFOptions * options = [IJKFFOptions optionsByDefault];

//(每条数据都有相对应主播的URL这个看你后台怎么命名了)

IJKFFMoviePlayerController * player = [[IJKFFMoviePlayerController alloc] initWithContentURLString:self.live.stream_addr withOptions:options];

self.player = player;

self.player.view.frame = self.view.bounds;

self.player.shouldAutoplay = YES;

[self.view addSubview:self.player.view];

}

4.接下来就是传赋值了在tableViewController里面写

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

DKTrendModel *live = self.live[indexPath.row];

DKPlayerViewController *playerVC = [[DKPlayerViewController alloc]init];

playerVC.live = live;

//    playerVC.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:playerVC animated:YES];

}

5.到这一步算基本完成简单直播了,接下来给你看看直播的效果


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容