Skip to content

commonProblems

isExist? edited this page Sep 22, 2016 · 4 revisions

1. 如何获取播放内容的总时长,当前播放时间和当前可播放长度?

2. 如何获得播放失败的错误码?

  • 首先需要注册监听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"];
        }
    }
}

3. reload功能的使用方法

  • reload的具体说明可参考高级功能中的reload特性
  • 调用该方法后,当播放器完成码流解析的准备工作,同样会发送 MPMediaPlaybackIsPreparedToPlayDidChangeNotification

4. 开始/结束缓冲的通知

  • 当播放器进入/退出缓冲状态时,都会发送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];
        }
}

5. 后台播放逻辑

  • 播放器支持后台播放,但是需要APP具有后台执行权限。具体设置方法是:工程的build选项->Capabilities标签->Background Mode选项设置为ON,并勾选Modes中的Audio, AirPlay and Picture in Picture选项,如下图所示: 后台权限配置
  • 当用户点击home按钮后,播放器进入后台继续读取数据并播放音频;当APP回到前台后,音频继续播放,图像渲染内容保持和音频同步
  • 如果在开启后台运行模式后,需要切换后台暂停,需要监听相关事件并主动调用pause操作;回到前台后主动调用play操作恢复播放

播放偶尔没有声音

请确认正确处理了MPMoviePlayerSuggestReloadNotification

Clone this wiki locally