【发布时间】:2019-04-23 20:26:40
【问题描述】:
我正在尝试添加通知观察器以检测何时移除耳机并且音乐停止播放。我关注了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)
}
}
【问题讨论】:
-
您在
.AVAudioSession.routeChangeNotification中有一个领先的.。删除将解决错误。 -
是的,我修好了,非常感谢
标签: swift avaudiosession