iOS 7使用AVPlayer在锁定屏幕上回放歌曲

前端之家收集整理的这篇文章主要介绍了iOS 7使用AVPlayer在锁定屏幕上回放歌曲前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我没有找到如何从锁定屏幕iOS 7回放歌曲的方式.音量滑块可用,所有按钮都正常工作,但上部滑块不可用!

AVPlayer是我正在使用的类.

任何帮助表示赞赏!

解决方法

您需要在“正在播放的信息”中设置此项.

每当项目(轨道)改变时,做类似的事情

NSMutableDictionary *nowPlayingInfo = [[NSMutableDictionary alloc] init];
[nowPlayingInfo setObject:track.artistName forKey:MPMediaItemPropertyArtist];
[nowPlayingInfo setObject:track.trackTitle forKey:MPMediaItemPropertyTitle];
...
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.player.rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo;

每当播放速率改变(播放/暂停)时,执行

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

您可以像这样获得当前的播放时间:

- (NSTimeInterval)currentPlaybackTime {
  CMTime time = self.player.currentTime;
  if (CMTIME_IS_VALID(time)) {
    return time.value / time.timescale;
  }
  return 0;
}

猜你在找的iOS相关文章