代码下载地点:https://github.com/pili-engineering/PLPlayerKit
体系请求: iOS7及以上版本
代码集成
体式格局一:CocoaPods的要领
直接在Podfile中增加
$ pod 'PLPlayerKit'
然后
$ pod install
或许
$ pod update
运转你工程的 Workspace,就集成终了了
体式格局二:非CocoaPods集成
详情请接见:非Cocoapods集成
疾速接入项目最先
在须要挪用的处所增加
#import <PLPlayerKit/PLPlayer.h>
初始化 PLPlayerOption
// 初始化 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 的设置是不是能够背景播放。