Reputation: 95
I'm trying to add notification observer to detect when headphones are removed and the music stop playing. I followed Audio Session. I'm trying to search Swift 4.2 syntax, changed some things from the original code from Apple but I still get this error:
Type of expression is ambiguous without more context
What am I doing wrong? Let me know if I need to add more code
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)
}
}
Upvotes: 0
Views: 529
Reputation: 6170
Remove the dot before AVAudioSession name: .AVAudioSession.routeChangeNotification
Upvotes: 1