MCPlayerKit 易用的播放元件,通用播放器

littleplayer發表於2019-01-05

當寫下2019·時,時間的匆忙撲面而來,願每一天是美好的一天。

MCPlayerKit

MCPlayerKit 介紹

從事移動端開發以來,一直在做音視訊類的開發,蘋果的播放器基本定製型太強,需要定製在開源有kxmovie,授權的vitamio等,但層次不一,經過幾年的發展,尤其是在這兩年直播等投資熱的趨勢下,音視訊技術逐漸趨於成熟,隨便找幾個庫就能解決燃眉之急,開源的力量更進一步的推進了技術的發展,我也想寫個簡單的專案,寫出一些心得。

MCPlayerKit 是基於AVPlayer和IJKPlayer做的一款播放核心播放模組,做IJKPlayer支援是因為AVPlayer對於一些格式協議的支援的補充,比如flv、RTMP等。

特點

  • PlayerKit高度抽象出播放層和渲染層
  • 耦合低,使用方便
  • 有很多使用案例

Installation

Installation with CocoaPods

To integrate MCPlayerKit into your Xcode project using CocoaPods, specify it in your Podfile: 如果純使用MCPlayerKit播放功能,請用

pod 'MCPlayerKit/Core' or pod 'MCPlayerKit'
複製程式碼

如果需要使用高度通用UI的,可以用

pod 'MCPlayerKit/GeneralPlayerUI'

複製程式碼
GeneralPlayerUI 樣式依賴於'MCStyle',Example/Classes/Modules/GeneralPlayer/MCPlayerGeneralController有他的詳細使用,
你可以通過json配置GeneralPlayerUI顏色、樣式、字型等。
複製程式碼

Run pod install

使用方法

1. 純播放功能,自定義UI

1.1 功能

    a. support AVPlayer & IJKPlayer
    b. 統一渲染頁面
複製程式碼

1.2 接入程式碼

- (MCPlayerKit *)playerKit {
    if (!_playerKit) {
        _playerKit = [[MCPlayerKit alloc] initWithPlayerView:self.playerView];
        _playerKit.playerCoreType = PlayerCoreAVPlayer;
        _playerKit.playerStatusDelegate = self;
    }
    return _playerKit;
}

//自定義渲染介面
- (MCPlayerView *)playerView {
    if (!_playerView) {
        CGFloat width = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        CGFloat height = width * 9 / 16.0f;
        _playerView = [[MCPlayerView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    }
    return _playerView;
}
[self.playerKit playUrls:@[@"http://aliuwmp3.changba.com/userdata/video/45F6BD5E445E4C029C33DC5901307461.mp4"]];
複製程式碼

2.GeneralPlayerUI 通用播放器UI

2.1 功能

    a. 旋轉
    b. support 2018 Devices
    c. loading
    b. 播放中斷重試
    c. 使用者可以自定義UI顏色、樣式、字型大小
TODO:
    I. 投屏UI
複製程式碼

2.2 接入程式碼

    //顏色配置
    [MCStyleManager share].colorStyleDataCallback = ^NSDictionary *(void) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CustomPlayerColor" ofType:@"json"];
        NSError *error;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:&error];
        NSAssert(!error, @"read json file error");
        return dictionary[@"data"];
    };

    //字型樣式配置
    [MCStyleManager share].fontStyleDataCallBack = ^NSDictionary *(void) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CustomPlayerFont" ofType:@"json"];
        NSError *error;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:&error];
        NSAssert(!error, @"read json file error");
        return dictionary[@"data"];
    };

    //圖片等配置
    [MCStyleManager share].styleDataCallback = ^NSDictionary *(void) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CustomPlayerStyle" ofType:@"json"];
        NSError *error;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableContainers error:&error];
        NSAssert(!error, @"read json file error");
        return dictionary[@"data"];
    };
    [[MCStyleManager share] loadData];

    [self.view addSubview:self.playerView];
    [self.playerView updatePlayerPicture:@"https://avatars0.githubusercontent.com/u/3861387?s=460&v=4"];
    [self.playerView updateTitle:@"Skipping code signing because the target does not have an Info.plist file. (in target 'App')"];
//    [self.playerKit playUrls:@[@"http://aliuwmp3.changba.com/userdata/video/45F6BD5E445E4C029C33DC5901307461.mp4"]];
    [self.playerKit playUrls:@[@"http://aaaaaliuwmp3.changba.com/userdata/video/45F6BD5E445E4C029C33DC5901307461.mp4"]];
    [self.playerView updateAction:self.playerKit];
    self.playerView.retryPlayUrl = ^NSString *(void) {
        return @"http://aliuwmp3.changba.com/userdata/video/45F6BD5E445E4C029C33DC5901307461.mp4";
    };
    
    - (MCPlayerKit *)playerKit {
        if (!_playerKit) {
            _playerKit = [[MCPlayerKit alloc] initWithPlayerView:self.playerView.playerView];
            _playerKit.playerCoreType = PlayerCoreAVPlayer;
        }
        return _playerKit;
    }
    
    - (MCPlayerGeneralView *)playerView {
        if (!_playerView) {
            CGFloat width = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            CGFloat height = width * 9 / 16.0f + [MCDeviceUtils xTop];
            _playerView = [[MCPlayerGeneralView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        }
        return _playerView;
    }
    
    具體參照 Example/Classes/Modules/GeneralPlayer/MCPlayerGeneralController
複製程式碼

2.3 Screenshot

MCPlayerKit_GeneralPlayerUI

類結構

PlayerKit --------------- 播放器核心,AVPlayer與IJKPlayer抽象
    -- Core               Player抽象與實現以及PlayerKit播放器控制
    -- Extend             輔助類
    -- PlayerView         播放器介面父類,Custom播放器介面View必須繼承與此類
    
GeneralPlayerUI----------- 通用播放元件UI
    -- Views               通用UI subviews
    -- MCPlayerGeneralView 通用播放介面

Commen ------------------- 通用類

複製程式碼

more

此專案會作為長期維護專案,接受各位指導。

License

These PlayerKit are available under the MIT license.

相關文章