2017-10-15 67 views
0

我目前正在嘗試同時監控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() 
      } 
     } 

回答

0

在此提及reddit thread smakusdod

iPhone有3個話筒...使用像陣列,這取決於照相機 是活動的,或者如果處於免提模式等等,如果蘋果想要的話, 他們可以將這些話筒用於立體聲錄音。例如,使用 底部話筒用於右聲道,頂部話筒用於左聲道。目前 他們不允許這樣做,而只是暴露一個單聲道。

由於蘋果只提供單聲道,意味着iOS開發人員無法同時訪問這些內置麥克風。

如果你覺得它應該是可能的,蘋果給你一個機會,讓他們知道: https://www.apple.com/feedback/iphone.html

+0

也許硬件不支持它。 –