通过蓝牙命令控制AVAudioRecorder

问题描述 投票:1回答:1

我有这个应用程序,允许用户记录周围的东西。然后,当他们具有蓝牙附件(例如耳机)时,他们应该可以通过蓝牙设备上的播放/暂停按钮开始/停止录制会话。

我已经使用MPRemoteCommandCenter成功实现了启动会话]

let rcCenter = MPRemoteCommandCenter.shared()
rcCenter.nextTrackCommand.isEnabled = false
rcCenter.nextTrackCommand.addTarget { _ in return .success }

rcCenter.previousTrackCommand.isEnabled = false
rcCenter.previousTrackCommand.addTarget { _ in return .success }

rcCenter.togglePlayPauseCommand.isEnabled = true
rcCenter.playCommand.isEnabled = true
rcCenter.pauseCommand.isEnabled = true
rcCenter.stopCommand.isEnabled = true

rcCenter.togglePlayPauseCommand.addTarget(self, action: #selector(remotePlayPauseAction(_:)))
rcCenter.playCommand.addTarget(self, action: #selector(remotePlayPauseAction(_:)))
rcCenter.pauseCommand.addTarget(self, action: #selector(remotePlayPauseAction(_:)))
rcCenter.stopCommand.addTarget(self, action: #selector(remotePlayPauseAction(_:)))

但是,它永远不会通过蓝牙进入我的暂停/停止动作。在设备控制台中看到此消息之前,我不知道发生了什么:

默认18:19:46.609673 +0700蓝牙从设备收到“获取播放状态”请求

默认18:19:46.671092 +0700蓝牙从设备接收到AVRCP播放命令

//发送音频会话状态,因为应用正在录制等等……

默认18:19:48.246780 +0700蓝牙AudioSendThread开始

当应用未录制时触发此操作->从蓝牙接收播放命令->开始录制->向蓝牙发送会话。

然后,我将再次按播放/暂停按钮并获得此日志:

默认18:20:09.650855 +0700蓝牙收到呼叫挂断事件(AT + CHUP)来自设备默认值18:20:09.651273+0700蓝牙已发现正在进行的虚拟通话-正在唤醒设备并通知上层。

=>看起来电话正在接收结束通话信号(请注意,没有正在进行的通话,只是会话中的录音)

所以,如何处理此事件?我尝试使用CallKit,但仍然没有用:

let callCenter = CXCallObserver()
callCenter.setDelegate(self, queue: nil)

func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
   print("Hi!") // not jump in!
}

更新

我尝试切换回旧样式处理程序:

UIApplication.shared.beginReceivingRemoteControlEvents()
becomeFirstResponder()
/////////////////
override func remoteControlReceived(with event: UIEvent?) {
    guard let event = event, event.type == .remoteControl else { return }
   // Start / pause actions
}

并且仍然得到相同的结果(可以像以前一样启动但无法停止,获得事件playcall hangup)。现在我的猜测已经切换到AVAudioSession的模式和类别,但仍然没有任何线索。

do {
    let session = AVAudioSession.shared()
    // What is the correct params here? 
    // try session.setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetoothA2DP, .allowBluetooth])
    // try session.setCategory(.record, options: .allowBluetooth)
    session.setActive(true)
} catch { print(error) }
ios bluetooth avaudiosession avaudiorecorder callkit
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.