iOS 7 rewind songs on lock screen using AVPlayer

前端 未结 4 1610
情话喂你
情话喂你 2020-12-17 00:55

I havn\'t found the way how to rewind song from lock screen iOS 7. Volume slider is available, all the buttons work correctly but the upper slider is not available!

4条回答
  •  自闭症患者
    2020-12-17 01:38

    You'll need to set this in the "now-playing info".

    Whenever the item (track) changes, do something like

    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;
    

    Whenever the playback rate changes (play/pause), do

    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;
    

    You can get the current playback time like this:

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

提交回复
热议问题