iOS直播之ijkplayer的集成與簡單使用(播放)

前言

下一個項目有一個直播的功能,所以需要提前研究一下,直接集成FFmpeg比較復(fù)雜坑也比較多(我是道聽途說的自己沒有嘗試??),朋友推薦了ijkplayer、七牛和vlc,于是我花了兩天時間研究了一下ijkplayer,過程非??部溃龅降膯栴}不下10個,中間曾一度想過放棄然后換其他的,但是看了一些文章之后還是從頭到尾做出來了,做事也算是有始有終,給自己一個贊??。其實集成的過程還是比較簡單的,按照教程一步一步來就可以了,如果正常的話可能一兩個小時就搞定了,我主要是想把我遇到的問題以及解決方法記錄一下與大家共同學(xué)習(xí)提高!

環(huán)境準備

集成ijkplayer需要電腦中安裝gityasm,然后好像還需要一個pkg-config(不確定),安裝git、yasmpkg-config可以使用homebrew。

檢查自己電腦是不是安裝了homebrew、gityasm可以打開終端依次輸入:

brew -v
git --version
yasm --version
pkg-config --verison 

我的已經(jīng)安裝了


環(huán)境配置成功

安裝成功可以忽略以下內(nèi)容直接查看第一章節(jié),如果沒有安裝成功請繼續(xù),首先打開終端

安裝homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果想卸載homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

homebrew的用法:

安裝軟件,如:brew install oclint
卸載軟件,如:brew uninstall oclint
搜索軟件,如:brew search oclint
更新軟件,如:brew upgrade oclint
查看安裝列表, 如:brew list
更新Homebrew,如:brew update

使用homebrew安裝git
brew install git
使用homebrew安裝yasm
brew install yasm
使用homebrew安裝pkg-config
brew install pkg-config

一、使用git克隆倉庫到本地

ijkplayer是bilibili的一個開源項目https://github.com/Bilibili/ijkplayer
在合適的位置新建文件夾(不想新建也可以的??),我是在桌面上新建文件夾命名為showcase,然后進入對應(yīng)文件夾

cd /Users/jizhigang/Desktop/showcase //這是我的路徑啊看準嘍

clone一個倉庫有兩種方式,使用https方式和SSH方式,我們選擇https方式,使用SSH方式繼續(xù)執(zhí)行可能出錯??

使用https地址
//克隆倉庫到指定位置并重命名為ijkplayer-ios
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios

有時候clone倉庫時會出現(xiàn)問題

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
    fatal: The remote end hung up unexpectedly
    fatal: early EOF
    fatal: index-pack failed

這個錯誤是因為項目太久,tag資源文件太大,我們clone時可以這樣

//克隆倉庫到指定位置并重命名為ijkplayer-ios,深度為1的代碼,具體什么意思我不是特別清楚就不亂解釋了啊,免得誤導(dǎo)別人
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios --depth=1
倉庫clone成功

然后進入文件夾ijkplayer-ios

cd ijkplayer-ios

新建分支latest并切換到新建的分支上

git checkout -B latest k0.8.8

我們平時開發(fā)最好不要在master分支上直接開發(fā),所以這里我按照教程新建了分支latest。但是我們不需要提交代碼,所以這里不新建分支也是可以的,新建與否都對后續(xù)各步驟沒有影響。

可以使用命令查看當前分支

git branch

可以看到當前有了兩個分支latestmaster,并且當前工作在latest分支上

分支創(chuàng)建并切換成功

配置編解碼器格式支持

默認為最少支持, 如果足夠你使用, 可以跳過這一步. 否則可以改為以下配置:

module-default.sh 更多的編解碼器/格式
module-lite-hevc.sh 較少的編解碼器/格式(包括hevc)
module-lite.sh 較少的編解碼器/格式(默認情況)

# 進入 config 目錄
cd config

# 刪除當前的 module.sh 文件
rm module.sh

# 可根據(jù)需要替換為`module-default.sh`, `module-lite-hevc.sh`, `module-lite.sh`
# 創(chuàng)建軟鏈接 module.sh 指向 module-lite-hevc.sh
ln -s module-lite-hevc.sh module.sh

cd ..
cd ios
sh compile-ffmpeg.sh clean

二、下載ffmpeg

./init-ios.sh //時間稍長一些
下載`ffmpeg`成功
添加 https 支持

最后會生成支持 https 的靜態(tài)文件 libcrypto.a 和 libssl.a, 如果不需要可以跳過這一步

# 獲取 openssl 并初始化
./init-ios-openssl.sh

cd ios

# 在模塊文件中添加一行配置 以啟用 openssl 組件
echo 'export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-openssl"' >> ../config/module.sh

./compile-ffmpeg.sh clean

然后進入ios文件夾中

cd ios

三、 編譯ffmpeg

# 如果下一步提示錯誤`xcrun: error: SDK "iphoneos" cannot be located`, 請執(zhí)行`sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/`, 再重新執(zhí)行下一步

# 編譯openssl, 如果不需要https可以跳過這一步
./compile-openssl.sh all

./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all //編譯

這里有可能遇到錯誤

xcrun -sdk iphoneos clang is unable to create an executable file.
C compiler test failed.

造成這個問題的原因有很多,這里我是因為xcode路徑問題引起的,解決方法:

sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer

這里還遇到了另一個問題

AS  libavcodec/arm/aacpsdsp_neon.o
./libavutil/arm/asm.S:50:9: error: unknown directive
        .arch armv7-a
        ^
make: *** [libavcodec/arm/aacpsdsp_neon.o] Error 1
make: *** Waiting for unfinished jobs....

最新的 Xcode 已經(jīng)弱化了對 32 位的支持, 解決方法:
compile-ffmpeg.sh 中刪除 armv7 , 修改如:
FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"
再重新執(zhí)行出現(xiàn)錯誤的命令: ./compile-ffmpeg.sh all

去掉armv7支持
去掉armv7支持
去掉armv7支持

然后再次執(zhí)行

./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all //編譯
編譯成功

打開ios/IJKMediaPlayer并運行

添加 openssl相關(guān)包以支持https

如果不使用 https, 可以跳過此步, 直接開始打包 framwork

如果使用 https, 那么需要手動給IJKMediaFramework添加 libcrypto.alibssl.a 文件, 默認不會添加

ps: 這兩個依賴庫的目錄為:ijkplayer-ios/ios/build/universal/lib, 只有進行了上面跟 openssl 相關(guān)的操作, 才會在這個目錄下有生成libcrypto.alibssl.a

步驟一
步驟二

然后以此打開build->universal->lib選擇libcrypto.alibssl.a

步驟三

打開工程可以看到


編譯FFmpeg成功

四、打包 framwork

target

大家會發(fā)現(xiàn)除了 IJKMediaFramework這個target, 還有一個叫 IJKMediaFrameworkWithSSL, 但是不推薦使用這個, 因為大部分基于 ijkplayer 的第三方框架都是使用的前者, 你把后者導(dǎo)入項目還是會報找不到包的錯誤, 就算你要支持 https 也推薦使用前者, 然后按照上一步添加 openssl即可支持

首先將debug改為release
debug改為release
分別編譯模擬器和真機
編譯模擬器
編譯真機

編譯的時候可能會遇到兩個問題,按照步驟注釋掉就可以了

注釋代碼
注釋代碼

注釋掉這兩行代碼之后再分別選擇真機和模擬器進行編譯command+b

成功之后可以看到


編譯成功了

選中IJKMediaFramework.framework右鍵show in finder可以看到

編譯成功了

合并真機和模擬器的framework,我們要合并的其實是這兩個文件
合并

首先我們cd到products文件夾中

進入products文件夾

然后執(zhí)行: lipo -create 真機framework路徑 模擬器framework路徑 -output 合并的文件路徑

lipo -create Release-iphoneos/IJKMediaFramework.framework/IJKMediaFramework Release-iphonesimulator/IJKMediaFramework.framework/IJKMediaFramework -output IJKMediaFramework

可以看到在products文件夾中生成了一個framework文件

合并

替換掉Release-iphoneos中的IJKMediaFramework,如圖

替換IJKMediaFramework

五、集成framwork并測試

新建工程并導(dǎo)入IJKMediaFramework.framework以及其他依賴庫

集成

新建控制器playerViewController然后從ViewController.swiftpush或者模態(tài)推出控制器進行測試,如果直接在跟控制器ViewController.swift中測試可能會有只出聲音沒有影像的問題

import UIKit

class playerViewController: UIViewController {

    var iPlayer:IJKFFMoviePlayerController? 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let options:IJKFFOptions = IJKFFOptions.byDefault()
        let url:URL = URL.init(string: "rtmp://live.hkstv.hk.lxdns.com/live/hks")!
 
        
        self.iPlayer = IJKFFMoviePlayerController.init(contentURL: url, with: options)
        var arm1 = UIViewAutoresizing.init(rawValue: 0)
        arm1.insert(UIViewAutoresizing.flexibleWidth)
        arm1.insert(UIViewAutoresizing.flexibleHeight) 
        self.iPlayer?.view.autoresizingMask = arm1
        self.iPlayer?.view.backgroundColor = UIColor.white
        self.iPlayer?.view.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
        self.iPlayer?.scalingMode = .aspectFit
        self.iPlayer?.shouldAutoplay = true
        self.view.autoresizesSubviews = true
        self.view.addSubview((self.iPlayer?.view)!)
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.iPlayer?.prepareToPlay() //準備
        self.iPlayer?.play() //播放
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.iPlayer?.pause()//暫停
//        self.iPlayer?.shutdown() //銷毀
    }

}

大功告成??


可以直播

demo地址https://github.com/jzglovewjr/ijk

參考地址
homebrew的安裝和使用:
https://blog.csdn.net/sir_coding/article/details/77509602
git報錯:
https://blog.csdn.net/it_liuchengli/article/details/77040806
執(zhí)行./compile-ffmpeg.sh all報錯 :
https://github.com/Bilibili/ijkplayer/issues/1160
armv7支持問題以及https支持問題:
http://www.itdecent.cn/p/9743a68c2939

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

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

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