2016-03-20 66 views
1

我正在用Swift編寫一個iOS應用程序,它可以記錄用戶的聲音,並且可以播放一些聲音效果,但問題在於使用內置iPhone麥克風時播放非常安靜。用耳機沒有問題。AVAudioEngine低音量

記錄代碼:

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() 

爲我解決方案的唯一痕跡是,原來的蘋果錄音應用程序做相同,但僅在右上角揚聲器圖標被禁用。雖然我無法找出那個揚聲器圖標的功能。

回答

2

好吧我終於找到了。問題是與AVAudioSession:

let session = AVAudioSession.sharedInstance() 
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker) 
+0

我想要的示例。如果我開始通過麥克風識別我的講話,那麼我試圖讓iPhone聽到那個可識別文本的聲音。這是工作。但是,聲音太低。你能指導我嗎? –

+0

https://stackoverflow.com/questions/45371931/swift-iphones-volume-is-low-when-trying-to-change-speech-to-iphones-voice-in 我的問題.... –