1

我正在开发一个 iOS 14 应用程序,它播放音频文件的片段供用户模仿。如果用户愿意,应用程序可以记录用户的反应并立即播放。用户还可以导出包含原始片段以及用户响应的录音。

我正在使用 AudioKit 4.11

因为用户可能永远不希望利用应用程序的录制功能,所以应用程序最初采用 .playback 的音频会话类别。如果用户想要使用录音功能,应用程序会触发标准的 Apple 对话框以请求麦克风访问权限,如果获得许可,则将会话类别切换为 .playAndRecord。

我发现,当会话类别为 .playback 且用户尚未授予麦克风权限时,我可以在蓝牙扬声器或 Jabra Elite 65t 蓝牙耳塞上收听应用程序的输出(当应用程序在真正的 iPhone。在下面的示例中,这是应用程序首次运行并且用户仅点击“播放声音”或“停止”的情况。

但是,一旦我点击“播放声音并记录响应”并授予麦克风访问权限,我就无法在蓝牙设备上收听应用程序的输出,无论当时适用的会话类别是否为 .playback(点击“播放声音并记录响应”)或 .playAndRecord(点击“播放声音”后)-除非我随后转到手机的隐私设置并将麦克风访问切换为关闭。只能通过手机的扬声器或插入的耳机进行播放。

在设置 .playAndRecord 的会话类别时,我尝试调用 .allowBluetoothA2DP 选项。Apple 的建议意味着这应该允许我在上述情况下通过蓝牙收听我的应用程序的声音(请参阅https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions/1771735-allowbluetootha2dp)。但是,我还没有发现这种情况。

下面的代码代表一个可运行的应用程序(尽管需要 AudioKit 4.11 的存在),它以简化的形式说明了问题。此处未显示的唯一元素是我添加到 info.plist 的 NSMicrophoneUsageDescription,以及我导入到项目中的文件“blues.mp3”。

内容视图:

import SwiftUI
import AudioKit
import AVFoundation

struct ContentView: View {

private var pr = PlayerRecorder()

var body: some View {
    VStack{
        Text("Play sound").onTapGesture{
            pr.setupforPlay()
            pr.playSound()
        }
        .padding()
        Text("Play sound and record response").onTapGesture{
            if recordingIsAllowed() {
                pr.activatePlayAndRecord()
                pr.startSoundAndResponseRecording()
            }
        }
        .padding()
        Text("Stop").onTapGesture{
            pr.stop()
        }
        .padding()
        
    }
}

func recordingIsAllowed() -> Bool {
    
    var retval = false
    AVAudioSession.sharedInstance().requestRecordPermission { granted in
        retval = granted
    }
    return retval
}

}

播放器记录器:

import Foundation
import AudioKit

class PlayerRecorder {

private var mic: AKMicrophone!
private var micBooster: AKBooster!
private var mixer: AKMixer!
private var outputBooster: AKBooster!
private var player: AKPlayer!
private var playerBooster: AKBooster!
private var recorder: AKNodeRecorder!
private var soundFile: AKAudioFile!
private var twentySecondTimer = Timer()

init() {
    AKSettings.defaultToSpeaker = true
    AKSettings.disableAudioSessionDeactivationOnStop = true
    AKSettings.notificationsEnabled = true
}

func activatePlayAndRecord() {
    do {
        try AKManager.shutdown()
    } catch {
        print("Shutdown failed")
    }
    setupForPlayAndRecord()
}


func playSound() {
    do {
        soundFile = try AKAudioFile(readFileName: "blues.mp3")
    } catch {
        print("Failed to open sound file")
    }
    do {
        try player.load(audioFile: soundFile!)
    } catch {
        print("Player failed to load sound file")
    }
    if micBooster != nil{
        micBooster.gain = 0.0
    }
    player.play()
}


func setupforPlay() {
    do {
        try AKSettings.setSession(category: .playback)
    } catch {
        print("Failed to set session category to .playback")
    }
    mixer = AKMixer()
    outputBooster = AKBooster(mixer)
    player = AKPlayer()
    playerBooster = AKBooster(player)
    playerBooster >>> mixer
    AKManager.output = outputBooster
    if !AKManager.engine.isRunning {
        try? AKManager.start()
    }
}


func setupForPlayAndRecord() {
    AKSettings.audioInputEnabled = true
    do {
        try AKSettings.setSession(category: .playAndRecord)
        /*  Have tried the following instead of the line above, but without success
         let options: AVAudioSession.CategoryOptions = [.allowBluetoothA2DP]
         try AKSettings.setSession(category: .playAndRecord, options: options.rawValue)
         
         Have also tried:
         try AKSettings.setSession(category: .multiRoute)
         */
    } catch {
        print("Failed to set session category to .playAndRecord")
    }
    
    mic = AKMicrophone()
    micBooster = AKBooster(mic)
    mixer = AKMixer()
    outputBooster = AKBooster(mixer)
    player = AKPlayer()
    playerBooster = AKBooster(player)
    mic >>> micBooster
    micBooster >>> mixer
    playerBooster >>> mixer
    AKManager.output = outputBooster
    micBooster.gain = 0.0
    outputBooster.gain = 1.0
    if !AKManager.engine.isRunning {
        try? AKManager.start()
    }
}

func startSoundAndResponseRecording() {
    // Start player and recorder. After 20 seconds, call a function that stops the player
    // (while allowing recording to continue until user taps Stop button).
    
    activatePlayAndRecord()
    playSound()
    
    // Force removal of any tap not previously removed with stop() call for recorder
    var mixerNode: AKNode?
    mixerNode = mixer
    for i in 0..<8 {
        mixerNode?.avAudioUnitOrNode.removeTap(onBus: i)
    }
    
    do {
        recorder = try? AKNodeRecorder(node: mixer)
        try recorder.record()
    } catch {
        print("Failed to start recorder")
    }
    twentySecondTimer = Timer.scheduledTimer(timeInterval: 20.0, target: self, selector: #selector(stopPlayerOnly), userInfo: nil, repeats: false)
}


func stop(){
    twentySecondTimer.invalidate()
    if player.isPlaying {
        player.stop()
    }
    if recorder != nil {
        if recorder.isRecording {
            recorder.stop()
        }
    }
    if AKManager.engine.isRunning {
        do {
            try AKManager.stop()
        } catch {
            print("Error occurred while stopping engine.")
        }
    }
    print("Stopped")
}

@objc func stopPlayerOnly () {
    player.stop()
    if !mic.isStarted {
        mic.start()
    }
    if !micBooster.isStarted {
        micBooster.start()
    }
    mic.volume = 1.0
    micBooster.gain = 1.0
    outputBooster.gain = 0.0
}
}
4

1 回答 1

0

setupForPlayAndRecord() 开头附近的三行代码解决了这个问题:

func setupForPlayAndRecord() {
    AKSettings.audioInputEnabled = true
    // Adding the following three lines solves the problem
    AKSettings.useBluetooth = true
    let categoryOptions: AVAudioSession.CategoryOptions = [.allowBluetoothA2DP]
    AKSettings.bluetoothOptions = categoryOptions
    do {
        try AKSettings.setSession(category: .playAndRecord)
    } catch {
        print("Failed to set session category to .playAndRecord")
    }
    mic = AKMicrophone()
    micBooster = AKBooster(mic)
    mixer = AKMixer()
    outputBooster = AKBooster(mixer)
    player = AKPlayer()
    playerBooster = AKBooster(player)
    mic >>> micBooster
    micBooster >>> mixer
    playerBooster >>> mixer
    AKManager.output = outputBooster
    micBooster.gain = 0.0
    outputBooster.gain = 1.0
    if !AKManager.engine.isRunning {
        try? AKManager.start()
    }
}
于 2020-12-10T05:59:16.700 回答