【问题标题】:How to stop audio playback when disconnecting audio devices in Swift app?在 Swift 应用程序中断开音频设备时如何停止音频播放?
【发布时间】:2022-12-18 02:13:53
【问题描述】:

我正在编写一个 Swift 应用程序,它可以从 Web Radio URL 流式传输音频,但我无法使其在音频路由更改时正常运行,尤其是在断开音频设备连接时。

我参考了 Apple 文档的 this 页面,但是我对示例中如何使用 headphonesConnected 变量以及如何不仅涵盖耳机,还涵盖蓝牙耳机、CarPlay 设备等其他设备感到困惑。

基本上我需要我的应用程序通过在外部音频设备(耳机、BT 耳机、CarPlay 设备)断开连接时停止播放来遵循 iOS 指南。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    先做一个观察者:

    NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: AVAudioSession.routeChangeNotification, object: nil)
    

    并实施 handleRouteChange:

     @objc func handleRouteChange(_ notification: Notification) {
              
             //bron: https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioHardwareRouteChanges/HandlingAudioHardwareRouteChanges.html#//apple_ref/doc/uid/TP40007875-CH5-SW3
            
              guard let userInfo = notification.userInfo,
              let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
              let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {return}
            
              switch reason {
                
              case .routeConfigurationChange:
              
                   let session = AVAudioSession.sharedInstance()
              
                   for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
                
                        play()
                
                        break
                   
                   }
                
                   for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.airPlay {
    
                        play()
    
                        break
    
                   }
    
                   for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.carAudio {
    
                        if isPlaying && autoplayCaraudio {
                             
                             play()
    
                        }
    
                        break
    
                   }
    
              case .oldDeviceUnavailable:
                
                   if let previousRoute = userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
    
                        for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
    
                             pause()
    
                             break
    
                        }
    
                        for output in previousRoute.outputs where output.portType == AVAudioSession.Port.airPlay {
    
                             pause()
    
                             break
    
                        }
    
                        for output in previousRoute.outputs where output.portType == AVAudioSession.Port.carAudio {
    
                             pause()
    
                             break
    
                        }
    
                   }
                
              default: ()
                
            }
            
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      相关资源
      最近更新 更多