七牛云直播 iOS播放器SDK接入流程

代码下载地址:https://github.com/pili-engineering/PLPlayerKit
系统要求: iOS7及以上版本git

代码集成

方式一:CocoaPods的方法

直接在Podfile中添加github

$ pod 'PLPlayerKit'

而后url

$ pod install

或者spa

$ pod update

运行你工程的 Workspace,就集成完毕了.net

方式二:非CocoaPods集成

详情请访问:非Cocoapods集成设计

快速接入项目开始
在须要调用的地方添加代理

#import <PLPlayerKit/PLPlayer.h>

初始化 PLPlayerOptioncode

// 初始化 PLPlayerOption 对象
PLPlayerOption *option = [PLPlayerOption defaultOption];

// 更改须要修改的 option 属性键所对应的值
[option setOptionValue:@15 forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];

初始化 PLPlayer视频

// 初始化 PLPlayer,self.URL是须要播放的直播的URL地址,目前支持 http (url 以 http:// 开头) 与 rtmp (url 以 rtmp:// 开头) 协议。
self.player = [PLPlayer playerWithURL:self.URL option:option];

// 设定代理 (optional)
self.player.delegate = self;

获取播放器的视频输出的 UIView 对象并添加为到当前 UIView 对象的 Subview对象

//获取视频输出视图并添加为到当前 UIView 对象的 Subview
[self.view addSubview:player.playerView];

开始/暂停操做

// 播放
[self.player play];

// 中止
[self.player stop];

// 暂停
[self.player pause];

// 继续播放
[self.player resume];

播放器状态获取

// 实现 <PLPlayerDelegate> 来控制流状态的变动
- (void)player:(nonnull PLPlayer *)player statusDidChange:(PLPlayerStatus)state {
    // 这里会返回流的各类状态,你能够根据状态作 UI 定制及各种其余业务操做
    // 除了 Error 状态,其余状态都会回调这个方法
}

- (void)player:(nonnull PLPlayer *)player stoppedWithError:(nullable NSError *)error {
    // 当发生错误时,会回调这个方法
}

音频部分的特别说明

由于 iOS 的音频资源被设计为单例资源,因此若是在 player 中作的任何修改,对外均可能形成影响,而且带来不能预估的各类问题。

为了应对这一状况,PLPlayerKit 采起的方式是检查是否能够播放及是否能够进入后台,而在内部不作任何设置。具体是经过扩展 AVAudioSession 来作到的,提供了两个方法,以下:

/*!
 * @description 检查当前 AVAudioSession 的 category 配置是否能够播放音频. 当为 AVAudioSessionCategoryAmbient,
 * AVAudioSessionCategorySoloAmbient, AVAudioSessionCategoryPlayback, AVAudioSessionCategoryPlayAndRecord
 * 中的一种时为 YES, 不然为 NO.
 */
+ (BOOL)isPlayable;

/*!
 * @description 检查当前 AVAudioSession 的 category 配置是否能够后台播放. 当为 AVAudioSessionCategoryPlayback,
 * AVAudioSessionCategoryPlayAndRecord 中的一种时为 YES, 不然为 NO.
 */
+ (BOOL)canPlayInBackground;

分辨能够检查是否能够播放以及当前 category 的设置是否能够后台播放。

相关文章
相关标签/搜索