我正在尝试让我的播客应用程序在锁定屏幕中显示内容,并尊重播放/暂停控件。这是我的代码:
- (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;
}
奇怪的是,这就是发生的事情:
- 响应和处理事件工作得很好
- 艺术家和标题显示在锁定屏幕上
- 进度条显示-:--,不显示播放进度
- 剧集的持续时间也不会出现。
这是获取当前经过时间的代码:
- (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;
}
}
有人知道怎么修这个东西吗?令我困惑的是,有些东西会出现在屏幕上,但当前的播放信息却没有。同样,接收和处理事件工作得很好。