前言
下一個項目有一個直播的功能,所以需要提前研究一下,直接集成FFmpeg比較復(fù)雜坑也比較多(我是道聽途說的自己沒有嘗試??),朋友推薦了ijkplayer、七牛和vlc,于是我花了兩天時間研究了一下ijkplayer,過程非常坎坷,遇到的問題不下10個,中間曾一度想過放棄然后換其他的,但是看了一些文章之后還是從頭到尾做出來了,做事也算是有始有終,給自己一個贊??。其實集成的過程還是比較簡單的,按照教程一步一步來就可以了,如果正常的話可能一兩個小時就搞定了,我主要是想把我遇到的問題以及解決方法記錄一下與大家共同學(xué)習提高!
環(huán)境準備
集成ijkplayer需要電腦中安裝git
和yasm
,然后好像還需要一個pkg-config
(不確定),安裝git
、yasm
和pkg-config
可以使用homebrew
。
檢查自己電腦是不是安裝了homebrew
、git
、yasm
可以打開終端依次輸入:
brew -v
git --version
yasm --version
pkg-config --verison
我的已經(jīng)安裝了
安裝成功可以忽略以下內(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í)行可能出錯??
//克隆倉庫到指定位置并重命名為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
然后進入文件夾ijkplayer-ios
cd ijkplayer-ios
新建分支latest
并切換到新建的分支上
git checkout -B latest k0.8.8
我們平時開發(fā)最好不要在
master
分支上直接開發(fā),所以這里我按照教程新建了分支latest
。但是我們不需要提交代碼,所以這里不新建分支也是可以的,新建與否都對后續(xù)各步驟沒有影響。
可以使用命令查看當前分支
git branch
可以看到當前有了兩個分支latest
和master
,并且當前工作在latest
分支上
配置編解碼器格式支持
默認為最少支持, 如果足夠你使用, 可以跳過這一步. 否則可以改為以下配置:
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 //時間稍長一些
添加 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
然后再次執(zhí)行
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all //編譯
打開ios/IJKMediaPlayer
并運行
添加 openssl
相關(guān)包以支持https
如果不使用
https
, 可以跳過此步, 直接開始打包framwork
如果使用
https
, 那么需要手動給IJKMediaFramework
添加libcrypto.a
和libssl.a
文件, 默認不會添加
ps: 這兩個依賴庫的目錄為:
ijkplayer-ios/ios/build/universal/lib
, 只有進行了上面跟openssl
相關(guān)的操作, 才會在這個目錄下有生成libcrypto.a
和libssl.a
然后以此打開build
->universal
->lib
選擇libcrypto.a
和libssl.a
打開工程可以看到
四、打包 framwork
大家會發(fā)現(xiàn)除了
IJKMediaFramework
這個target
, 還有一個叫IJKMediaFrameworkWithSSL
, 但是不推薦使用這個, 因為大部分基于ijkplayer
的第三方框架都是使用的前者, 你把后者導(dǎo)入項目還是會報找不到包的錯誤, 就算你要支持https
也推薦使用前者, 然后按照上一步添加openssl
即可支持
首先將debug
改為release
分別編譯模擬器和真機
編譯的時候可能會遇到兩個問題,按照步驟注釋掉就可以了
注釋掉這兩行代碼之后再分別選擇真機和模擬器進行編譯command+b
成功之后可以看到
選中IJKMediaFramework.framework
右鍵show in finder
可以看到
合并真機和模擬器的framework
,我們要合并的其實是這兩個文件
首先我們cd到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
,如圖
五、集成framwork
并測試
新建工程并導(dǎo)入IJKMediaFramework.framework
以及其他依賴庫
新建控制器playerViewController
然后從ViewController.swift
中push
或者模態(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.lxweimin.com/p/9743a68c2939