由于2016年直播行業(yè)特別火驶兜,也越來(lái)越多的人想做直播舀凛】“猓可是視頻直播一些傳輸協(xié)議(RTMP基于HTTP協(xié)議),視頻采用什么H.264壓縮猛遍,音頻采用ACC等等這些太復(fù)雜了馋记。所以我們需要集成第三方号坡,下面我就為大家詳細(xì)講解集成步驟,不足之處歡迎交流 抗果。 ? ? ? ?QQ1725865030?
1.框架名字: Bilibili/ijkplayer
注意事項(xiàng):把Bilibili/ijkplayer下載下來(lái)你會(huì)發(fā)現(xiàn)這個(gè)文件很小筋帖,打開(kāi)iOS里面的IJKMediaDemo也報(bào)錯(cuò)找不到#include "libavformat/avformat.h"這個(gè)文件。這個(gè)時(shí)候我們要在終端下載Bilibili/ijkplayer冤馏,具體步驟GitHub里面有說(shuō)明日麸,因?yàn)槲募容^大就不一一講解了。(弄不好的朋友可以加上方的@QQ我會(huì)把源碼也給你逮光,源碼里面的步驟我標(biāo)記的比較清晰)
2.接下來(lái)就是添加依賴的庫(kù)了
3.接下來(lái)我們就要拷貝一些我們需要的代碼了(這里我們需要簡(jiǎn)單的直播代碼我已經(jīng)拷貝下來(lái)了你直接創(chuàng)建一個(gè)繼承于ViewController控制器就行了代箭,把下面這些代碼全部粘貼到你的.m文件里)
#import "DKPlayerViewController.h"http://第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;
//注冊(cè)直播需要用的通知
[self installMovieNotificationObservers];
//準(zhǔn)備播放
[self.player prepareToPlay];
}
//第1.3步
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//關(guān)閉直播
[self.player shutdown];
//移除
[self removeMovieNotificationObservers];
}
#pragma mark Install Movie Notifications
//1.4
/* Register observers for the various movie object notifications. */
-(void)installMovieNotificationObservers
{
//監(jiān)聽(tīng)網(wǎng)絡(luò)環(huán)境,監(jiān)聽(tīng)緩沖方法
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateDidChange:)
name:IJKMPMoviePlayerLoadStateDidChangeNotification
object:_player];
//監(jiān)聽(tīng)直播完成回調(diào)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:IJKMPMoviePlayerPlaybackDidFinishNotification
object:_player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mediaIsPreparedToPlayDidChange:)
name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:_player];
//監(jiān)聽(tīng)用戶主動(dòng)操作
[[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,緩沖結(jié)束可以播放
//? ? MPMovieLoadStatePlaythroughOK? = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES 緩沖結(jié)束自動(dòng)播放
//? ? 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, 直播結(jié)束
//? ? MPMovieFinishReasonPlaybackError, 直播錯(cuò)誤
//? ? 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設(shè)置播放的player
- (void)initPlayer {
IJKFFOptions * options = [IJKFFOptions optionsByDefault];
//(每條數(shù)據(jù)都有相對(duì)應(yīng)主播的URL這個(gè)看你后臺(tái)怎么命名了)
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.接下來(lái)就是傳賦值了在tableViewController里面寫(xiě)
-(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.到這一步算基本完成簡(jiǎn)單直播了涕刚,接下來(lái)給你看看直播的效果