我有录音/播放应用程序。但是,当用户使用常规有线iPhone耳机上的播放/暂停按钮时,我想暂停播放。所以我实现了远程事件的处理:
// MARK: Overrides
internal extension AppDelegate {
override func remoteControlReceived(with event: UIEvent?) {
super.remoteControlReceived(with: event)
/* some other logic */
}
}然后我开始接收application: didFinishLaunchingWithOptions:中的远程事件
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
application.beginReceivingRemoteControlEvents()
becomeFirstResponder()
/* some other logic */
return true
}但不管怎样,remoteControlReceived(with event: UIEvent?)永远不会触发。
我也尝试过MPRemoteCommandCenter:
MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
return .success
}不会触发。
接受Swift或objective-c答案:)
怎么啦?或者我应该在.plist中添加一些东西?
发布于 2017-06-29 00:02:31
好了,我找到原因了..。我正在使用我的应用程序来混合它与其他应用程序的声音,所以我使用mixWithOthers选项激活音频会话:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers])但是,如果我使用的是mixWithOthers,应用程序会停止接收远程事件。这意味着,如果我想从头戴式耳机接收toggle play/pause事件,我不能使用此选项:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)这很奇怪,但这就是我们所拥有的。所以在我的例子中,我不能使用耳机按钮:(
发布于 2017-06-28 00:13:55
使用此代码来完成您的工作。App的文档可以在here中找到
MPRemoteCommandCenter.sharedCommandCenter().togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayStop))
func togglePlayStop(){
//isPlaying = !isPlaying
}https://stackoverflow.com/questions/44784739
复制相似问题