如何仅使用Airpod的麦克风而不停止其他应用程序正在播放的音乐?

时间:2019-01-17 23:52:32

标签: ios swift airpods

我的应用程序只想访问Airpod的麦克风(而不是扬声器),但是当我尝试从我的应用程序开始录音时,如果正在播放,它就会变成音乐。我如何只使用Airpod的麦克风。我不想停止任何其他应用程序正在播放的音乐。

我尝试执行以下操作。该代码段实际上可以从Airpod的麦克风中获取输入,但会停止播放任何其他歌曲。

try recordingSession.setCategory(.playAndRecord, mode: .measurement, options: .allowBluetooth)
try recordingSession.setActive(true)


audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record() 

我不想停止任何其他应用程序正在播放的音乐。我只想使用Airpod的麦克风

1 个答案:

答案 0 :(得分:0)

尝试一下

try recordingSession.setCategory(.playAndRecord, mode: .measurement, options: [.allowBluetoothA2DP, .mixWithOthers])

然后在您AppDelegate中将这段代码添加到didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    try! AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.mixWithOthers)

    return true
}