ios 内置麦克风录音只有一个声道声音

Posted

技术标签:

【中文标题】ios 内置麦克风录音只有一个声道声音【英文标题】:ios recording with built-in mic only have one channel sound 【发布时间】:2016-04-22 12:38:15 【问题描述】:

我正在使用 AVAudioEngine 来捕捉用户的声音并对其应用一些效果。使用耳机麦克风录制时,一切正常。但是用手机内置mic录音,再通过耳机播放声音时,只有左侧耳塞有声音,看来内置mic只有单声道输入。那么我该如何解决这个问题呢?这是我的一些代码:

func connectNode()
  engine.connect(engine.inputNode!, to: reverbNode, format:reverbNode.outputFormatForBus(0))
  engine.connect(reverbNode, to: delayNode, format: delayNode.outputFormatForBus(0))
  engine.connect(delayNode, to: distortion, format: distortion.outputFormatForBus(0))
  engine.connect(distortion, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0))
  engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0))


func recordToFile()
  setSessionRecord()
  recordSetting[AVFormatIDKey] = NSNumber(unsignedInt: UInt32(kAudioFormatMPEG4AAC))
  recordSetting[AVNumberOfChannelsKey] = NSNumber(int: 2)
  var file: AVAudioFile!
  do
    try file = AVAudioFile(forWriting: URLFor(fileName)!, settings: recordSetting)
  catch let error as NSError
  print("error:\(error)")
  
  engine.mainMixerNode.installTapOnBus(0, bufferSize: 1024, format: file.processingFormat)  (buffer, time) -> Void in
  try! file.writeFromBuffer(buffer)
  


func playbackRecord()
  setSessionPlayback()
  var file:AVAudioFile!
  file = try! AVAudioFile(forReading:URLFor(fileName)!)
  let audioFormat = file.processingFormat
  let audioFrameCount = UInt32(file.length)
  let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)
  try! file.readIntoBuffer(audioFileBuffer, frameCount: audioFrameCount)
  player.scheduleBuffer(audioFileBuffer, atTime: nil, options: .Interrupts, completionHandler: print(self.player.stop()))
  if(!player.playing)
    player.play()
  else
    print("stop")
    player.stop()
   

【问题讨论】:

【参考方案1】:

我找到了解决此问题的方法。只需修改此方法,为引擎提供单通道格式,然后一切都会好起来的。

func connectNode()
  let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatFloat32, sampleRate: 44100.0, channels:AVAudioChannelCount(1), interleaved: false)
  engine.connect(engine.inputNode!, to: reverbNode, format:format)
  engine.connect(reverbNode, to: delayNode, format: format)
  engine.connect(delayNode, to: distortion, format: format)
  engine.connect(distortion, to: engine.mainMixerNode, format: format)
  engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0))

【讨论】:

以上是关于ios 内置麦克风录音只有一个声道声音的主要内容,如果未能解决你的问题,请参考以下文章

如何在平板电脑/智能手机上启用立体声录音,尽管有两个内置麦克风,但只产生单声道?

FastICA 无法在真实世界的录音中分离声音信号

如何确定录音源

iPhone/iPad上的双通道录音:耳机+内置麦克风

mp3格式转换后没有了立体声

MediaRecorder.AudioSource参数