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

标签 ios objective-c avfoundation avplayer ios7

我还没有找到如何从锁定屏幕 iOS 7 倒回歌曲的方法。音量 slider 可用,所有按钮都可以正常工作,但上部 slider 不可用!

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 7 使用 AVPlayer 在锁定屏幕上倒回歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733040/

相关文章:

ios - 使用Facebook登录时,FBSDKLoginManagerLoginResult isCancelled为true

iphone - 如何使用自定义图像制作计数器?

objective-c - 更改单元格内的 UIButton 图像

iOS逐帧视频播放向前/向后

objective-c - 尝试使用图层指令翻转视频时,AVFoundation AVAssetExportSession 会生成空白视频

ios - 如何删除NSCache中的所有缓存?

ios - 不同的初始化方法被调用

objective-c - 经纬度的iOS位置

objective-c - 如何打印字体的字符集?

ios - 使用 GPU 清除 CVPixelBufferRef