IJKPlayer使用

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。如需轉載請標註! https://blog.csdn.net/qq_34347441/article/details/54140079

直播拉流:

按照https://github.com/Bilibili/ijkplayer上的readme將IJKPlayer打包好,(已打包好的https://pan.baidu.com/s/1c1kB3S 提取碼: 32hn)

將打包好的IJKPlayer包拖入工程(是oc的, 引入頭文件; 是swift的, 建立橋文件), 導入相應的庫:

在控制器中寫入如下代碼(基本設置代碼, 如需擴展請自己添加)

import UIKit
class ViewController: UIViewController {
    // 在模擬器上聲音和畫面不同步或者比較卡頓的現象在真機沒事的,不用太擔心。
    fileprivate var player: IJKFFMoviePlayerController?
    override func viewDidLoad() {
        super.viewDidLoad()
        let options:IJKFFOptions = IJKFFOptions.byDefault();
        // 幀速率(fps)(可以改,確認非標準楨率會導致音畫不同步,所以只能設定爲15或者29.97)
        options.setOptionIntValue(15, forKey: "max-fps", of: kIJKFFOptionCategoryPlayer);
        #if DEBUG
           //  設置報告日誌
            IJKFFMoviePlayerController.setLogReport(true);
            //  設置日誌的級別爲Debug
            IJKFFMoviePlayerController.setLogLevel(k_IJK_LOG_DEBUG);
        #else
            //  設置不報告日誌
            IJKFFMoviePlayerController.setLogReport(false);
            //  設置日誌級別爲信息
            IJKFFMoviePlayerController.setLogLevel(k_IJK_LOG_INFO);
        #endif
        self.player = IJKFFMoviePlayerController(contentURL: URL(string: "視頻拉流地址"), with: options);
        // 設置適配橫豎屏(設置四邊固定,長寬靈活)
        self.player?.view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight];
        self.player?.view.frame = self.view.bounds;
        // 填充fill
        self.player?.scalingMode = IJKMPMovieScalingMode.aspectFill;
        // 設置自動播放(必須設置爲NO,防止自動播放,才能更好的控制直播的狀態)
        self.player?.shouldAutoplay = true;
        self.view.autoresizesSubviews = true;
        self.view.addSubview(self.player!.view);
        // 播放
        self.player?.prepareToPlay();
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

另附demo(爲swift版本, 跟oc差不多): 

https://github.com/songhaisheng/SGLive

參考的博客地址: 

http://www.jianshu.com/p/a7d9ed02cf40

http://www.jianshu.com/p/4f21af680c19

http://www.jianshu.com/p/b8db6c142aad

簡書博客地址

https://www.jianshu.com/u/3c7c13f3dc6b

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章