关闭音频播放?

Turn off audio playing?

我正在使用 AVPlayer 播放来自 URL 的音频。一切正常,但我不知道如何在不同 ViewController 中停止当前音频播放 我不想在 viewWillDisappear() 中调用 player.pause() 因为我想停止它在特定情况下,但我仍然希望在应用程序处于非活动状态时在后台播放音频。

我在 Xcode 应用程序设置中启用了此功能,以便在应用程序处于非活动状态时进行后台播放

2nd question: Can I improve this class or is everything ok?

protocol AVPlayerServiceDelegate {
    func playerDidUpdateCurrentPlayingTime(_ time: CMTime)

}

class AVPlayerService {
    static let instance = AVPlayerService()

    private var audioPlayer: AVPlayer!
    public weak var delegate: AVPlayerServiceDelegate?

    func setupPlayer(forURL url: URL) {
        let playerItem: AVPlayerItem = AVPlayerItem(url: url)
        audioPlayer = AVPlayer(playerItem: playerItem)
        audioPlayer.play()

        audioPlayer.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, preferredTimescale: 1), queue: DispatchQueue.main) { (CMTime) -> Void in
            if self.audioPlayer.currentItem?.status == .readyToPlay {
                self.delegate?.playerDidUpdateCurrentPlayingTime(self.audioPlayer.currentTime())
            }
        }
    }

    var isPlaying: Bool {
        return audioPlayer.isPlaying
    }

    var isPaused: Bool {
        return audioPlayer.isPaused
    }

    var rate: Float {
        return audioPlayer.rate
    }

    func seek(to time: CMTime) {
        audioPlayer.seek(to: time)
    }

    func seekToBeginning() {
        audioPlayer.seek(to: CMTimeMake(value: 0, timescale: 1))
    }

    func replay() {
        audioPlayer.seek(to: CMTimeMake(value: 0, timescale: 1))
        audioPlayer.play()
    }

    func pause() {
        if let audioPlayer = audioPlayer {
            audioPlayer.pause()
        }
    }

    func play() {
        audioPlayer.play()
    }

    func freshInstance() {
        audioPlayer = AVPlayer()
    }

    var assetDuration: CMTime {
        get {
            return audioPlayer.currentItem!.asset.duration
        }
    }
    }

您需要一种机制来与 player 对象对话,以便对其执行操作。
有很多方法可以解决。

就个人而言,我会为处理实例化、加载音频资源、播放、暂停等的播放器创建专用 class
然后我要么让这个 class 成为一个单例,这样我就可以从任何地方访问它,或者注入它并将它传递给其他 classes 将被使用的地方。

基本示例:

class MyAudioPlayer {
    static let shared = MyAudioPlayer()
    
    private var player: AVAudioPlayer?
    
    func load(_ url: URL) {
        player = try? AVAudioPlayer(contentsOf: url)
    }

    func play(url: URL) {
        load(url: url)
        player?.play()
    }

    func pause() {
        player?.pause()
    }

    func stop() {
        player?.stop()
    }     
}

从应用内的任何地方发送操作:

MyAudioPlayer.shared.play(someURL)
MyAudioPlayer.shared.pause()

至于即使应用程序未处于活动状态也能在后台播放需要(可能在 AppDelegate 中)

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, 
                                                 with: .duckOthers)
try? AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.beginReceivingRemoteControlEvents()