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
上面的代码将无限循环音频。但是,在锁定屏幕控件上,当轨道结束循环时,轨道时间停留在轨道的末尾(而不是重置):
如何更新nowPlayingInfo显示的曲目当前时间?