表达式类型不明确,没有更多上下文AVAudioSession

时间:2018-11-21 09:43:44

标签: swift avaudiosession

我正在尝试添加通知观察器,以检测何时删除了耳机并且音乐停止播放。我关注了Audio Session。我正在尝试搜索Swift 4.2语法,从Apple的原始代码中更改了一些内容,但仍然收到此错误:

  

表达式类型不明确,没有更多上下文

我在做什么错?让我知道是否需要添加更多代码

override func awakeFromNib() {
    super.awakeFromNib()

    NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChanged), name: .AVAudioSession.routeChangeNotification, object: nil)
    //Error this line: Type of expression is ambiguous without more context

}

@objc func audioRouteChanged(notification: Notification) {

    guard let userInfo = notification.userInfo else { return }
    guard let reason = userInfo[AVAudioSessionRouteChangeReasonKey] as? Int else { return }

    if reason == AVAudioSession.RouteChangeReason.oldDeviceUnavailable.hashValue {
        // headphones plugged out
        // continue playback or change the play/pause icon
        playPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal)
        miniPlayPauseButton.setImage(#imageLiteral(resourceName: "play"), for: .normal)
    }
}

1 个答案:

答案 0 :(得分:1)

删除AVAudioSession name: .AVAudioSession.routeChangeNotification之前的点