音频循环后立即更新nowPlayingInfo currentTime

时间:2018-12-16 10:02:12

标签: ios swift swift4 avaudioplayer mpnowplayinginfocenter

    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

上面的代码将无限循环音频。但是,在锁定屏幕控件上,当轨道结束循环时,轨道时间停留在轨道的末尾(而不是重置):

enter image description here

如何更新nowPlayingInfo显示的曲目当前时间?

0 个答案:

没有答案