Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
内置麦克风的ios录音只有一个声道_Ios_Swift_Audio_Audio Recording_Avaudioengine - Fatal编程技术网

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

内置麦克风的ios录音只有一个声道,ios,swift,audio,audio-recording,avaudioengine,Ios,Swift,Audio,Audio Recording,Avaudioengine,我用AVAudioEngine捕捉用户的声音并对其应用一些效果。当用耳机麦克风录音时,一切都很顺利。但当使用手机内置麦克风录音,并通过耳机播放声音时,只有左侧耳塞有声音,似乎内置麦克风只有单声道输入。那么我如何解决这个问题呢?以下是我的一些代码: func connectNode(){ engine.connect(engine.inputNode!, to: reverbNode, format:reverbNode.outputFormatForBus(0)) engine.conn

我用AVAudioEngine捕捉用户的声音并对其应用一些效果。当用耳机麦克风录音时,一切都很顺利。但当使用手机内置麦克风录音,并通过耳机播放声音时,只有左侧耳塞有声音,似乎内置麦克风只有单声道输入。那么我如何解决这个问题呢?以下是我的一些代码:

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

我找到了解决这个问题的方法。只要修改这个方法,给引擎一个单通道格式,然后一切都会好起来

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