2016-03-20 64 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 我的问题.... –