我正在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()
播放代码:
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()
对我来说唯一的解决方案是原始苹果录音应用程序的功能相同,但只有当右上角扬声器图标被禁用时才会这样做。虽然我无法找出扬声器图标的作用。
答案 0 :(得分:4)
好吧我终于找到了。问题在于AVAudioSession:
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)