由于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.到这一步算基本完成简单直播了,接下来给你看看直播的效果