Swift 4 AVFoundation - 同时录制多个音频源

Swift 4 AVFoundation - 同时录制多个音频源

问题描述:

我目前正在尝试同时监控iPhone的每个内置麦克风的音频输入。Swift 4 AVFoundation - 同时录制多个音频源

在下述代码,我捡了话筒,我想监视/米(底部,正面或背面麦克风)这行代码:

try recordingSession.setInputDataSource(recordingSession.inputDataSources?[2]) 

不幸的是,好像我只能设置一个输入数据源用于我的音频会话。

也许有没有一种方法来设置每个记录通道的输入数据源?

我已经尝试去探索其他解决方案,比如AVAudioEngine,但是由于没有很多关于这个主题的Swift资源,所以我很难分析要走哪条路。

import UIKit 
import AVFoundation 

     class ViewController: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate { 

      @IBOutlet var recordButton: UIButton! 
      var recordingSession: AVAudioSession! 
      var audioPlayer: AVAudioPlayer! 
      var audioRecorder: AVAudioRecorder! 

      var timer: Timer! 

      override func viewDidLoad() { 
       recordingSession = AVAudioSession.sharedInstance() 

       do { 
        try recordingSession.setCategory(AVAudioSessionCategoryMultiRoute) 
        //try recordingSession.setMode(AVAudioSessionModeMeasurement) 
        try recordingSession.setActive(true) 


        try recordingSession.setInputDataSource(recordingSession.inputDataSources?[2]) 
        try recordingSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker) 
        recordingSession.requestRecordPermission() { [unowned self] allowed in 
         DispatchQueue.main.async { 
          if allowed { 

          } else { 
           // failed to record! 
          } 
         } 
        } 
       } catch { 
        print("NOT ALLOWED") 
        // failed to record! 
       } 

      } 

      @objc func updateMeters(){ 
       audioRecorder.updateMeters() 


       print("CHANNEL 0 PEAK : \(audioRecorder.peakPower(forChannel: 0))") 
       print("CHANNEL 1 PEAK : \(audioRecorder.peakPower(forChannel: 1))") 
       print(audioRecorder.averagePower(forChannel: 0)) 
       print(audioRecorder.currentTime) 
      } 


      func startRecording() { 
       let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a") 

       let settings = [ 
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC), 
        AVSampleRateKey: 12000, 
        AVNumberOfChannelsKey: 2, 
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue 
       ] 

       do { 

        audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings) 
        audioRecorder.delegate = self 
        audioRecorder.isMeteringEnabled = true 
        audioRecorder.record() 

        recordButton.setTitle("Tap to Stop", for: .normal) 

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateMeters), userInfo: nil, repeats: true) 
       } catch { 
        finishRecording(success: false) 
       } 
      } 

      func getDocumentsDirectory() -> URL { 
       let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) 
       let documentsDirectory = paths[0] 
       return documentsDirectory 
      } 

      func finishRecording(success: Bool) { 
       timer.invalidate() 
       audioRecorder.stop() 
       audioRecorder = nil 

       if success { 
        recordButton.setTitle("Tap to Re-record", for: .normal) 
       } else { 
        recordButton.setTitle("Tap to Record", for: .normal) 
        // recording failed :(
       } 
      } 

      @IBAction func recordTapped() { 
       if audioRecorder == nil { 
        startRecording() 
       } else { 
        finishRecording(success: true) 
       } 
      } 

      func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { 
       if !flag { 
        finishRecording(success: false) 
       } 
      } 

      @IBAction func play(_ sender: UIButton) { 
       let fileURL = getDocumentsDirectory().appendingPathComponent("recording.m4a") 
        self.audioPlayer = try! AVAudioPlayer(contentsOf: fileURL) 
        self.audioPlayer.prepareToPlay() 
        self.audioPlayer.delegate = self 
        self.audioPlayer.play() 
      } 
     } 

在此提及reddit thread smakusdod

iPhone有3个话筒...使用像阵列,这取决于照相机 是活动的,或者如果处于免提模式等等,如果苹果想要的话, 他们可以将这些话筒用于立体声录音。例如,使用 底部话筒用于右声道,顶部话筒用于左声道。目前 他们不允许这样做,而只是暴露一个单声道。

由于苹果只提供单声道,意味着iOS开发人员无法同时访问这些内置麦克风。

如果你觉得它应该是可能的,苹果给你一个机会,让他们知道: https://www.apple.com/feedback/iphone.html

+0

也许硬件不支持它。 –