-
Notifications
You must be signed in to change notification settings - Fork 146
loadState
Franken Zeng edited this page May 22, 2017
·
4 revisions
使用加载情况,可以监控播放过程中的卡顿情况。
说明视频当前加载状态,如加载中,加载完成等
播放过程中的卡顿和两个因素相关:
- 观看拉流的速率
- 播放器缓存长度
请仔细阅读:播放延时控制wiki, wiki中提供了如何控制延时来实现卡顿率和延时的平衡。对于拉流环境恶劣的用户,建议通过卡顿率红线来关闭直播追赶来降低卡顿率。
typedef NS_OPTIONS(NSUInteger, MPMovieLoadState) {
MPMovieLoadStateUnknown = 0,
MPMovieLoadStatePlayable = 1 << 0,
MPMovieLoadStatePlaythroughOK = 1 << 1,
MPMovieLoadStateStalled = 1 << 2,
};
- MPMovieLoadStateUnknown - 加载情况未知
- MPMovieLoadStatePlayable - 加载完成,可以播放
- MPMovieLoadStatePlaythroughOK - 加载完成,如果shouldAutoPlay为YES,将自动开始播放
- MPMovieLoadStateStalled - 加载中
- 通过loadState属性(只读)获取当前加载状态
- 当接收到MPMoviePlayerLoadStateDidChangeNotification通知时应调用loadState获取一下当前播放器的加载状态;
调用播放器属性bufferEmptyCount获取从开始播放到现在缓冲的次数
_player.bufferEmptyCount;
调用播放器属性bufferEmptyDuration获取从开始播放到现在每次缓冲的时间和
_player.bufferEmptyDuration;
当出现MPMovieLoadStateStalled 时,说明当前播放出现卡顿,画面停止等待拉流满足开播条件。
此时bufferEmptyCount计数加一。
当出现MPMovieLoadStatePlayable 或者MPMovieLoadStatePlaythroughOK,说明经历卡顿后,拉流填充buffer完成,播放状态转为开始播放,画面和声音进入渲染。
此时bufferEmptyDuration 会统计此时卡顿耗时,并累积起来。
if (MPMoviePlayerLoadStateDidChangeNotification == notify.name) {
NSLog(@"player load state: %ld", (long)_player.loadState);
if (MPMovieLoadStateStalled & _player.loadState) {
stat.text = [NSString stringWithFormat:@"player start caching"];
NSLog(@"player start caching");
}
if (_player.bufferEmptyCount &&
(MPMovieLoadStatePlayable & _player.loadState ||
MPMovieLoadStatePlaythroughOK & _player.loadState)){
NSLog(@"player finish caching");
NSString *message = [[NSString alloc]initWithFormat:@"loading occurs, %d - %0.3fs",
(int)_player.bufferEmptyCount,
_player.bufferEmptyDuration];
[self toast:message];
}
}