我正在构建一个应用程序,用户可以在其中选择要播放的音频文件。即使屏幕被锁定,它通常也可以播放。问题在于,在用户更改了音频文件之后,第一个音频文件仍在后台播放,并且与用户要收听的文件重叠。
let player: AVPlayer = {
let avPlay = AVPlayer()
avPlay.allowsExternalPlayback = false
avPlay.automaticallyWaitsToMinimizeStalling = false
return avPlay
}()
fileprivate func setupLockScreenCurrentTime(){
var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo
// nowPlayingInfo = nil
guard let currentItem = player.currentItem else{return}
let durationInSecs = CMTimeGetSeconds(currentItem.duration)
// let elapsed = CMTimeGetSeconds(player.currentTime())
nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = durationInSecs
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
fileprivate func playSermon(){
let credential = URLCredential(user: "1234user" , password: "password", persistence: .forSession)
let protectionSpace = URLProtectionSpace(host: "example.com", port: 443, protocol: "https", realm: "Restricted", authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
URLCredentialStorage.shared.setDefaultCredential(credential, for: protectionSpace)
let ssurl = SermonHelper.sharedInstance.url
let urlNew:String = ssurl.replacingOccurrences(of: " ", with: "%20")
guard let url = URL(string: urlNew) else {return}
let playerItem = AVPlayerItem(url: url)
player.replaceCurrentItem(with: playerItem)
player.isMuted = false
player.play()
observePlayerCurrentTime()
}
fileprivate func setupRemoteControl(){
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.player.play()
self.enlargeEpisodeImageView()
self.playPauseButton.setImage(UIImage(named: "my_pause2"), for: .normal)
self.setupElapsedTime(rate: 1)
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.player.pause()
self.shrinkEpisodeImageView()
self.playPauseButton.setImage(UIImage(named: "my_play2"), for: .normal)
self.setupElapsedTime(rate: 0)
return .success
}
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
if self.player.timeControlStatus == .playing {
self.player.pause()
self.shrinkEpisodeImageView()
self.playPauseButton.setImage(UIImage(named: "my_play2"), for: .normal)
}
else {
self.player.play()
self.enlargeEpisodeImageView()
self.playPauseButton.setImage(UIImage(named: "my_pause2"), for: .normal)
}
return .success
}
commandCenter.skipForwardCommand.isEnabled = true
commandCenter.skipForwardCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.seekToCurrentTime(delta: 15)
let t = CMTimeAdd(self.player.currentTime(), CMTimeMake(15, 1))
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(t)
return .success
}
commandCenter.skipBackwardCommand.isEnabled = true
commandCenter.skipBackwardCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.seekToCurrentTime(delta: -15)
let n = CMTimeAdd(self.player.currentTime(), CMTimeMake(-15, 1))
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(n)
return .success
}
}
我希望正在播放的音频被用户选择的新音频文件替换,但是我同时从两个文件中获取音频。
答案 0 :(得分:0)
在playSermon()
中用新声音替换当前声音之前停止播放当前声音时,代码中没有任何显示。尝试做到这一点。很有可能会解决您的问题。