簡體   English   中英

播放外部音頻時暫停 AVAudioPlayer

[英]Pause AVAudioPlayer when external audio is playing

我目前正面臨一個問題,如果你們中的一些人遇到了這個問題並提出了一個簡單的解決方案,我會很高興。

所以基本上,問題是:當一些外部聲音進入時,我有什么方法可以暫停正在進行的 AVAudioPlayer,例如:音樂、來電、通知等。

我知道為了檢測呼叫,有這個新的 CXCallObserverDelegate 可以幫助我們,但是對於音樂和其余部分,是否有任何簡單的解決方案來解決所有這些問題?

在代碼方面,一切正常,播放音頻沒有任何問題。 我有一個自定義路徑和一個錯誤,錯誤返回 nil。

NSError *avPlayerError = nil; NSData* mp3Data = [[NSData alloc] initWithContentsOfFile: path options: NSDataReadingMappedIfSafe error: &avPlayerError];

我正在將此代碼用於我的應用程序:

在 AppDelegate 中, didFinishLaunchingWithOptions為任何傳入中斷添加觀察者

NotificationCenter.default.addObserver(self, selector: #selector(self.onAudioSessionEvent(noti:)), name: Notification.Name.AVAudioSessionInterruption, object: nil)

當任何中斷發生時,這個函數將被調用

@objc func onAudioSessionEvent(noti:Notification){
    if noti.name == Notification.Name.AVAudioSessionInterruption
    {
        if let value = noti.userInfo![AVAudioSessionInterruptionTypeKey] as? NSNumber {
            if value.uintValue == AVAudioSessionInterruptionType.began.rawValue {
                print("start interrupt")


            }else{
                print("end interrupt")

            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM