1

我正在尝试让我的播客应用程序在锁定屏幕中显示内容,并尊重播放/暂停控件。这是我的代码:

- (void)playEpisodeAtIndex:(NSInteger)index {

    _currentIndex = index;
    TalkPodEpisode *episode = self.playlist[index];
    self.player = [[AVPlayer alloc] initWithURL:episode.url];
    [self.player play];
    _playing = YES;

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    NSDictionary *lockScreenInfo = @{MPMediaItemPropertyTitle:self.currentEpisode.title,
                                 MPMediaItemPropertyArtist: @"The Talk Pod",
                                 MPNowPlayingInfoPropertyPlaybackRate:[NSNumber numberWithDouble:self.player.rate],
                                          MPNowPlayingInfoPropertyElapsedPlaybackTime:[NSNumber numberWithDouble:[self currentPlaybackTime]]};
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo;


}

奇怪的是,这就是发生的事情:

  1. 响应和处理事件工作得很好
  2. 艺术家和标题显示在锁定屏幕上
  3. 进度条显示-:--,不显示播放进度
  4. 剧集的持续时间也不会出现。

这是获取当前经过时间的代码:

- (NSTimeInterval)currentPlaybackTime {

    CMTime time = self.player.currentTime;
    if (CMTIME_IS_VALID(time)) {

        return time.value/time.timescale;

    }

    return 0;

}

这是切换播放和暂停的代码:

- (void)playPause {

    if (self.player) {

        if (_playing) {

            [self.player pause];
            _playing = NO;

        } else {

            [self.player play];
            _playing = YES;

        }

        NSMutableDictionary *lockScreenInfo = [[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo mutableCopy];
        lockScreenInfo[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithDouble:self.player.rate];
        lockScreenInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:[self currentPlaybackTime]];
        [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo;

    }

}

有人知道怎么修这个东西吗?令我困惑的是,有些东西会出现在屏幕上,但当前的播放信息却没有。同样,接收和处理事件工作得很好。

4

1 回答 1

0

可能您需要在 nowPlayingInfo 字典中设置 Playback Duration 键。查看我的帖子以了解另一个问题,它在我的应用程序中运行良好。 堆栈:在控制中心显示寻道持续时间

于 2015-04-06T09:50:00.167 回答