AVAudioEngine低音量

Ern*_*zyk 2 iphone avfoundation swift

我正在用Swift编写一个iOS应用程序,该应用程序可以录制用户的语音,然后可以播放一些语音效果,但是问题是,使用内置的iPhone麦克风时,播放非常安静。使用耳机没有问题。

记录代码:

let recordingName = "my_audio.m4a"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
print(filePath)

let recordSettings: [String: AnyObject] = [
    AVFormatIDKey: Int(kAudioFormatAppleLossless),
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey : 320000,
    AVNumberOfChannelsKey: 2,
    AVSampleRateKey : 44100.0
]

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: recordSettings)

audioRecorder.meteringEnabled = true
audioRecorder.updateMeters()
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
audioRecorder.record()
Run Code Online (Sandbox Code Playgroud)

播放代码:

playerNode.stop()
playerNode.volume = 1.0
audioEngine.stop()
audioEngine.reset()

audioEngine.attachNode(playerNode)
audioEngine.attachNode(audioUnitTime)
audioEngine.connect(playerNode, to: audioUnitTime, format: receivedAudio.processingFormat)
audioEngine.connect(audioUnitTime, to: audioEngine.outputNode, format: receivedAudio.processingFormat)

playerNode.scheduleFile(receivedAudio, atTime: nil, completionHandler: nil)
try! audioEngine.start()
playerNode.play()
Run Code Online (Sandbox Code Playgroud)

对我而言,唯一的解决方案是,原始的苹果语音录制应用程序执行相同的操作,但是仅当右上角的扬声器图标被禁用时。尽管我无法确定扬声器图标的作用。

Ern*_*zyk 5

好吧,我终于找到了。问题出在AVAudioSession上:

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)
Run Code Online (Sandbox Code Playgroud)