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

iWh*_*Buy 7 objective-c avfoundation ios avplayer ios7

我没有找到如何从锁定屏幕iOS 7回放歌曲的方式.音量滑块可用,所有按钮都正常工作,但上部滑块不可用!

AVPlayer是我正在使用的类.

任何帮助表示赞赏!

chr*_*ris 11

你需要在"现在播放的信息"中设置它.

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

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;
Run Code Online (Sandbox Code Playgroud)

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

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;
Run Code Online (Sandbox Code Playgroud)

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

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

  • 抱歉,这还没有回答如何在锁定的屏幕上使滑杆可拖动以控制轨道内的位置的问题.默认音乐播放器具有垂直刻度标记较大且可以拖动的功能.它是一个尴尬的界面,与滑动解锁功能冲突,很难抓住微小的刻度标记(进度拇指),但它是默认应用程序可用的功能,似乎没有一个接口定制开发的音乐应用.-rrh (4认同)
  • `MPNowplayingInfo`可以与任何玩家一起使用.用`AVPlayer`这样做. (3认同)