iOS IJKMediaPlayer庫開發(fā)直播(拉流)

ijkplayer 是一款做視頻直播的框架, 基于ffmpeg, 支持 Android 和 iOS。

一. 下載ijkplayer

ijkplayer下載地址:https://github.com/Bilibili/ijkplayer

(1)下載后解壓,找到 Ijkplayer Demo,運(yùn)行

(2) Ijkplayer Demo 報(bào)錯(cuò) 'libavformat/avformat.h' file not found

原因:因?yàn)閘ibavformat是ffmpeg中的庫,而ijkplayer是基于ffmpeg這個(gè)庫的,因此需要導(dǎo)入ffmpeg

解決:通過腳本下載ffmpeg

image

init-ios.sh腳本的作用:下載ffmpeg源碼

如何執(zhí)行init-ios.sh腳本文件

1、找到init-ios.sh腳本文件

image

2、打開終端,cd進(jìn)入到ijkplayer的目錄中

image

3、輸入./init-ios.sh,就會(huì)執(zhí)行當(dāng)前腳本了。

image

4、腳本執(zhí)行結(jié)束后,在ijkplayer中就有ffmpeg了。

image

(3)下載好ffmpeg源碼后,再次運(yùn)行 Ijkplayer Demo

發(fā)現(xiàn)還是報(bào)'libavformat/avformat.h' file not found錯(cuò)誤

原因:執(zhí)行init-ios.sh,僅僅是下載源碼,但是源碼并沒有參與編譯,需要把源碼編譯成.a文件

1、 Ijkplayer Demo依賴于IJKMediaPlayer庫

image

2、打開 IJKMediaPlayer庫,查看下源碼

image

3、發(fā)現(xiàn)工程中,.a文件報(bào)紅,說明文件不存在

image

4、解決:查看ijkplayer的README.md

image

5、找到compile-ffmpeg.sh腳本

image

5、cd到compile-ffmpeg.sh腳本文件的目錄下

image

6、執(zhí)行./compile-ffmpeg.sh clean

image

7、執(zhí)行./compile-ffmpeg.sh all

image

8、執(zhí)行完./compile-ffmpeg.sh all

image

(4)處理好以上問題,再次運(yùn)行 Ijkplayer Demo

Ijkplayer Demo 報(bào)錯(cuò) 'libavutil/imgutils.h' file not found

原因:蘋果在 XCode10 和 iOS12 中移除了 libstdc++ 這個(gè)庫,由 libc++ 這個(gè)庫取而代之。

解決:下載libstdc++.tbd文件,然后移進(jìn)工程中。

image

下載鏈接:GitHub - Heaven7th/Xcode10-libstdc-6.0.9.tbd: Xcode10中缺少的libstdc++6.0.9.tbd庫

(5)處理好以上問題,再次運(yùn)行 Ijkplayer Demo

運(yùn)行成功后,進(jìn)入頁面如下

image

二. 集成ijkplayer到工程中

(1)制作ijkplayer.framework

1、找到 IJKMediaPlayer,打開相應(yīng)的工程。

image

2、發(fā)現(xiàn)該工程缺少「Products」文件夾

用途:該文件夾是用來保存.ipa、.a、.framework等文件,缺少這個(gè),我們就找不到生成的.framewor庫。

處理:找到 IJKMediaPlayer.xcodeproj ,右擊「顯示包內(nèi)容」,打開 project.pbxproj 文件,搜索到如下內(nèi)容:

image

將mainGroup對(duì)應(yīng)的value串,替換到productRefGroup相應(yīng)的value串,使得mainGroup和productRefGroup的value一樣。

保存并使用xcode重新打開該項(xiàng)目,就能看到「Products」文件夾。

image

3、接著點(diǎn)擊IJKMediaFramework出現(xiàn)選擇框,選擇edit scheme,如下圖所示:

image

4、可以選擇兩種模式:調(diào)試或開發(fā)

image

5、分別在模擬器和真機(jī)(Generic iOS Device也可以)上編譯

image
image

6、打開framework所在的目錄,如下圖所示:

image

7、查看生成的framework

image

8、將真機(jī)和模擬機(jī)合并到一起

合并命令:lipo -create 真機(jī)文件路徑 模擬器文件路徑 -output 真機(jī)文件路徑

真機(jī)文件路徑:指framework可執(zhí)行文件的路徑

image

將紅框中的文件拉進(jìn)終端,終端會(huì)顯示該文件的路徑信息。

合并報(bào)錯(cuò):have the same architectures (arm64) and can't be in the same fat output file

原因:真機(jī)framework中包含 armv7 arm64,模擬機(jī)framework中包含 i386 x86_64,但是Xcode 12以后模擬機(jī)編譯生成的framework中也會(huì)包含arm64,所以就會(huì)報(bào)arm64錯(cuò)誤。

可以通過lipo -info framework可執(zhí)行文件路徑,檢測framework包含哪些平臺(tái)

image

解決:在 Target->BuildSetting->Architectures->Excluded Architectures 中,在Any iOS Simulator SDK 后面添加arm64,這樣編譯出來的包中就不會(huì)包含arm64了。

image

問題:有時(shí)候編譯的模擬器framework只有arm64了,x86_64的沒有了。

解決:依然在Target->BuildSetting中,搜索VALID_ARCHS,在這里是描述你編譯時(shí)需要包含哪些架構(gòu)的,在后面添加上x86_64即可。

image

Xcode 13 新增User-Defined 設(shè)置(如下圖)Build Setting -> "+" :通過這個(gè),可以添加屬性

image

補(bǔ)充目前為止Apple移動(dòng)設(shè)備默認(rèn)指令集:

armv6:iPhone2G/3G、iPod 1G/2G

armv7:iPhone 3GS/4/4s、iPod 3G/4G、iPad 1G/2G/3G、iPad mini

armv7s:iPhone5、iPhone5c、iPad4

arm64:iPhone5s及以后、iPad Air、iPad mini2

9、把以上的問題處理完,再次通過lipo -create命令合并,最后真機(jī)sdk就是我們合并后的sdk,支持真機(jī)和模擬器使用。

image

(2)往工程導(dǎo)入合并后的IJKMediaFramework.framework以及相關(guān)依賴框架以及相關(guān)依賴框架,如下圖:

image

(3)代碼測試


#import <IJKMediaFramework/IJKFFMoviePlayerController.h>
@interface PlayLiveViewController ()
@property(nonatomic,strong)IJKFFMoviePlayerController * player;
@end

@implementation PlayLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    IJKFFOptions *options = [IJKFFOptions optionsByDefault]; //使用默認(rèn)配置
    NSURL * url = [NSURL URLWithString:@"http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear2/prog_index.m3u8"];
    self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options]; //初始化播放器,播放在線視頻或直播(RTMP)
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.player.view.frame = self.view.bounds;
    self.player.scalingMode = IJKMPMovieScalingModeAspectFit; //縮放模式
    self.player.shouldAutoplay = YES; //開啟自動(dòng)播放
    
    self.view.autoresizesSubviews = YES;
    [self.view addSubview:self.player.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.player prepareToPlay];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.player shutdown];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

IJKMediaPlayer庫 :https://gitee.com/xu_sheng_jie/ijkmedia-player.git

到此,IJKMediaPlayer初步集成完畢。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容