-
Notifications
You must be signed in to change notification settings - Fork 146
commonProblems
isExist? edited this page Sep 22, 2016
·
4 revisions
- 获取播放内容总时长,请使用duration;
- 获取当前播放时间,请使用currentPlaybackTime,标记的是播放器已经播放的时长;
- 获取当前可播放长度,请使用playableDuration,标记的是播放器缓冲的时间,会稍大于currentPlaybackTime,与currentPlaybackTime的差值则是缓冲长度
- 首先需要注册监听MPMoviePlayerPlaybackDidFinishNotification通知
- 接受到此通知时,从notify中提取MPMoviePlayerPlaybackDidFinishReasonUserInfoKey来获得具体的结束原因,若结束原因为MPMovieFinishReasonPlaybackError,则可以进一步提取error来获得具体错误码
- 具体的结束原因请见MPMovieFinishReason
- 具体的错误码请参考KSYMPErrorCode
- 具体代码如下:
- (void)setupObservers
{
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handlePlayerNotify:)
name:(MPMoviePlayerPlaybackDidFinishNotification)
object:nil];
}
-(void)handlePlayerNotify:(NSNotification*)notify
{
if (!_player) {
return;
}
if (MPMoviePlayerPlaybackDidFinishNotification == notify.name) {
NSLog(@"player finish state: %ld", (long)_player.playbackState);
int reason = [[[notify userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
stat.text = [NSString stringWithFormat:@"player finish"];
}else if (reason == MPMovieFinishReasonPlaybackError){
stat.text = [NSString stringWithFormat:@"player Error : %@", [[notify userInfo] valueForKey:@"error"]];
}else if (reason == MPMovieFinishReasonUserExited){
stat.text = [NSString stringWithFormat:@"player userExited"];
}
}
}
- reload的具体说明可参考高级功能中的reload特性
- 调用该方法后,当播放器完成码流解析的准备工作,同样会发送 MPMediaPlaybackIsPreparedToPlayDidChangeNotification
- 当播放器进入/退出缓冲状态时,都会发送MPMoviePlayerLoadStateDidChangeNotification通知
- 接收到MPMoviePlayerLoadStateDidChangeNotification通知后,应通过loadState属性(只读)获取当前的加载状态;
- 具体代码如下:
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];
}
}
- 播放器支持后台播放,但是需要APP具有后台执行权限。具体设置方法是:工程的build选项->Capabilities标签->Background Mode选项设置为ON,并勾选Modes中的Audio, AirPlay and Picture in Picture选项,如下图所示:
- 当用户点击home按钮后,播放器进入后台继续读取数据并播放音频;当APP回到前台后,音频继续播放,图像渲染内容保持和音频同步
- 如果在开启后台运行模式后,需要切换后台暂停,需要监听相关事件并主动调用pause操作;回到前台后主动调用play操作恢复播放
请确认正确处理了MPMoviePlayerSuggestReloadNotification