我正在尝试让我的播客应用在锁定屏幕中显示内容,以及尊重播放/暂停控件。这是我的代码:
- (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;
}
}
任何人都知道如何解决这个问题?令我困惑的是,一些的东西出现在屏幕上,但当前的播放信息却没有。同样,接收和处理事件也很合适。
答案 0 :(得分:0)
可能需要在nowPlayingInfo字典中设置播放持续时间键。 看看我的帖子中的另一个问题,它在我的应用程序中运行正常。 Stack : Show seek track duration on control center