一、使用第三方ijkPlayer框架開發直播
1、去到B站得github主頁,找到ijkplayer項目,下載源碼 ijkplayer下載地址
2、Demo的使用以及如何編譯Demo
請移步如何快速的開發一個完整的iOS直播app(播放篇)
其實裏面講的很詳細,我也是參照這個寫的Demo。然後在他的基礎上加了一點東西。(不廢話我直接上我的代碼)
先看看效果圖
特點
1、純代碼Masonry佈局
2、集成ijkplayer第三方庫,實現拉流播放
3、打包ijkplayer靜態庫,實現release版真機模擬機的包
4、實現點贊與送禮物特效
二、代碼詳解
其實使用第三方的庫簡單的實現拉流還是比較簡單的。
只要將打包好的IJKMediaFramework包拉到項目,然後導入系統運行所必要的包,比如下圖
1、導入頭文件
import<IJKMediaFramework/IJKMediaFramework.h>
2、實現直播畫面
// 拉流地址
NSURL *url = [NSURL URLWithString:_model.stream_addr];
// 創建IJKFFMoviePlayerController:專門用來直播,傳入拉流地址就好了
IJKFFMoviePlayerController *playerVc = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
// 準備播放
[playerVc prepareToPlay];
// 強引用,反正被銷燬
_player = playerVc;
// 自動調整自己的寬度和高度
playerVc.view.frame = [UIScreen mainScreen].bounds;
playerVc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:playerVc.view atIndex:1];
[playerVc setScalingMode:IJKMPMovieScalingModeAspectFill];
3、實現點贊與禮物
/**
點贊
*/
-(void)showTheLove:(UIButton *)sender{
_heartSize = 36;
DMHeartFlyView* heart = [[DMHeartFlyView alloc]initWithFrame:CGRectMake(0, 0, _heartSize, _heartSize)];
[self.view addSubview:heart];
CGPoint fountainSource = CGPointMake(_heartSize + _heartSize/2.0, self.view.bounds.size.height - _heartSize/2.0 - 10);
heart.center = fountainSource;
[heart animateInView:self.view];
// button點擊動畫
CAKeyframeAnimation *btnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
btnAnimation.values = @[@(1.0),@(0.7),@(0.5),@(0.3),@(0.5),@(0.7),@(1.0), @(1.2), @(1.4), @(1.2), @(1.0)];
btnAnimation.keyTimes = @[@(0.0),@(0.1),@(0.2),@(0.3),@(0.4),@(0.5),@(0.6),@(0.7),@(0.8),@(0.9),@(1.0)];
btnAnimation.calculationMode = kCAAnimationLinear;
btnAnimation.duration = 0.3;
[sender.layer addAnimation:btnAnimation forKey:@"SHOW"];
}
/**
送禮物
*/
- (void)showMyPorsche918 {
CGFloat durTime = 3.0;
UIImageView *porsche918 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"porsche"]];
//設置汽車初始位置
porsche918.frame = CGRectMake(0, 0, 0, 0);
[self.view addSubview:porsche918];
//給汽車添加動畫
[UIView animateWithDuration:durTime animations:^{
porsche918.frame = CGRectMake(ScreenW * 0.5 - 100, ScreenH * 0.5 - 100 * 0.5, 240, 120);
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(durTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
[UIView animateWithDuration:0.5 animations:^{
porsche918.alpha = 0;
} completion:^(BOOL finished) {
[porsche918 removeFromSuperview];
}];
});
//煙花
CALayer *fireworksL = [CALayer layer];
fireworksL.frame = CGRectMake((ScreenW - 250) * 0.5, 100, 250, 50);
fireworksL.contents = (id)[UIImage imageNamed:@"gift_fireworks_0"].CGImage;
[self.view.layer addSublayer:fireworksL];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(durTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
//沒找到設置透明度的方法,有創意可以自己寫
// fireworksL.alpha = 0;
} completion:^(BOOL finished) {
[fireworksL removeFromSuperlayer];
}];
});
_fireworksL = fireworksL;
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 1; i < 3; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"gift_fireworks_%d",i]];
[tempArray addObject:image];
}
_fireworksArray = tempArray;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
}
static int _fishIndex = 0;
- (void)update {
_fishIndex++;
if (_fishIndex > 1) {
_fishIndex = 0;
}
UIImage *image = self.fireworksArray[_fishIndex];
_fireworksL.contents = (id)image.CGImage;
}
三、需要注意的地方
界面消失,一定要記得停止播放,不然會引起內存泄漏。
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// 界面消失,一定要記得停止播放
[_player pause];
[_player stop];
[_player shutdown];
[_timer invalidate];
_timer = nil;
}
四、代碼分享
傳送門:LiveDemo
這個代碼缺少最重要的自己打包的IJKMediaFramework,需要自己打包,不過我也學習分享文章中的方法 ,將文件分開,你們可以自己打包,也可以去我的百度雲中下載。
傳送門: IJKMediaFramework
最後,如果有什麼疑問或者百度雲鏈接或者GitHub源碼鏈接出現問題,可以留言或者通過郵箱[email protected]找我