gpt4 book ai didi

ios - 有没有办法在音频 B 也开始播放后修复音频 A 的播放?

转载 作者:行者123 更新时间:2023-11-28 07:24:31 25 4
gpt4 key购买 nike

我正在构建一个应用程序,用户可以在其中选择要播放的音频文件。这通常有效,即使在屏幕锁定时也能播放。问题在于,在用户更改音频文件后,第一个音频文件仍在后台播放,并与用户想要收听的文件重叠。

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
}
}

我希望正在播放的音频被用户选择的新音频文件替换,但我从同时播放的两个文件中获取了音频。

最佳答案

当您在 playSermon() 中用新声音替换当前声音之前停止播放当前声音时,代码中的任何地方都不会指示。尝试这样做。这很可能会解决您的问题。

关于ios - 有没有办法在音频 B 也开始播放后修复音频 A 的播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com