RXAVPlayerKit
简介
RXAVPlayerKit 是一个基于 AVPlayer 和由 ReactiveObjC 驱动的轻量级封装,通过信号而不是代理/块来传递播放器状态。它旨在使 AVPlayer 相关逻辑更清晰,代码更简洁,更易于维护和调试。
重要:需要 ReactiveObjC。
用法
- 初始化 RXAVPlayerView
#import <ReactiveObjC/ReactiveObjC.h>
#import <RXAVPlayerKit/RXAVPlayerKit.h>
self.playerView = [RXAVPlayerView new];
[someView addSubview:self.playerView];
- 播放控制
NSURL *videoUrl = [NSURL URLWithString:@"http://aliuwmp3.changba.com/userdata/video/45F6BD5E445E4C029C33DC5901307461.mp4"];
[self.playerView playWithURL:videoUrl];
// other controls:
[self.playerView play];
[self.playerView pause];
[self.playerView stop];
[self.playerView mute:true];
[self.playerView seekToTime:0];
- 播放状态更新
@weakify(self)
[[self.playerView.currentTimeUpdated deliverOnMainThread] subscribeNext:^(NSNumber *_Nullable x) {
@strongify(self)
NSTimeInterval currentTime = x.doubleValue;
// do something to update currentTime label and/or slider...
}
[[[[self.playerView.durationUpdated deliverOnMainThread] distinctUntilChanged] ignore:@(NAN)] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
NSTimeInterval duration = x.doubleValue;
// do something to update duration label...
}];
[[self.playerView.playbackPlaying deliverOnMainThread] subscribeNext:^(NSURL * _Nullable x) {
@strongify(self)
// player is requested to play...
}];
[[self.playerView.playbackPaused deliverOnMainThread] subscribeNext:^(NSURL * _Nullable x) {
@strongify(self)
// player is paused...
}];
[[self.playerView.playbackStopped deliverOnMainThread] subscribeNext:^(NSURL * _Nullable x) {
@strongify(self)
// player is stopped and currentTime is ZERO...
}];
[[self.playerView.playbackEnded deliverOnMainThread] subscribeNext:^(NSURL * _Nullable x) {
@strongify(self)
// player has played to end...
}];
[[self.playerView.playbackStalled deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// player is stalled for buffering...
}];
[[self.playerView.muteStatusUpdated deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// player (not the device) is muted or unmuted;
}];
[[[self.playerView.volumeUpdated deliverOnMainThread] distinctUntilChanged] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// volume of player (not the device) is changed...
}];
- 寻道操作
@weakify(self)
[[self.playerView.seekStarted deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// player started to seek...
}];
[[self.playerView.seekCompleted deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// seek operation is completed...
}];
[[self.playerView.seekInterrupted deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
// last seek operation is interrupted by current seek operation...
}];
- 引发错误
@weakify(self)
[[self.playerView.errorRaised deliverOnMainThread] subscribeNext:^(NSError * _Nullable x) {
@strongify(self)
// do something to handle error...
}];
- 数据传输更新
@weakify(self)
[[self.playerView.downloadSpeedUpdated deliverOnMainThread] subscribeNext:^(NSNumber * _Nullable x) {
@strongify(self)
long long bytesPerSecond = x.longLongValue;
// do something...
}];
- 其他
@weakify(self)
[[self.playerView.audioSessionRouteChanged deliverOnMainThread] subscribeNext:^(NSDictionary * _Nullable x) {
@strongify(self)
// headphones plugged-in/out...
}];
其他信号请参考示例演示。
示例
要运行示例项目,请克隆仓库,并在 Example 目录中首先运行 pod install
需求
- iOS 8.0+;
- ReactiveObjC v3.1.0;
安装
RXAVPlayerKit可以通过CocoaPods获取。要安装它,只需在您的Podfile中添加以下行:
pod 'RXAVPlayerKit'
作者
许可证
RXAVPlayerKit在MIT许可证下可用。更多信息请参阅LICENSE文件。