2
    try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default);
    try? AVAudioSession.sharedInstance().setActive(true);

    let path = Bundle.main.path(forResource: "sample", ofType: "mp3")!
    let player = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
    player.numberOfLoops = -1;
    player.play();

    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.addTarget { _ in
        player.play()
        return .success
    }
    commandCenter.pauseCommand.addTarget { _ in
        player.pause()
        return .success
    }

    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.duration
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

The above code will loop the audio infinitely. However, on the lock screen control, the track time is stuck at the end of the track (instead of resetting) when the track finishes a loop:

enter image description here

How do I update the track current time shown by nowPlayingInfo?

Avery235
  • 4,756
  • 12
  • 49
  • 83

0 Answers0