【问题标题】:How to select bottom microphone for IOS video app?如何为 IOS 视频应用选择底部麦克风?
【发布时间】:2017-04-24 21:37:34
【问题描述】:

我在网上找到了这个示例代码:https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html

我正在尝试将输入麦克风从默认麦克风更改为 iPhone 上的底部麦克风。有没有人在 Swift 中有过这方面的经验?我发现的唯一示例是在 Obj-C 中,并且在我实现它们时会导致错误。我正在使用苹果的 AVCam 示例应用程序作为参考,音频部分包含在下面。

// Add audio input.
    do {
        let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
        let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice)

        if session.canAddInput(audioDeviceInput) {
            session.addInput(audioDeviceInput)
        }
        else {
            print("Could not add audio device input to the session")
        }
    }
    catch {
        print("Could not create audio device input: \(error)")
    }

【问题讨论】:

    标签: ios iphone swift avfoundation


    【解决方案1】:

    您应该尝试使用以下方法设置会话类别:

    session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker, error: nil)
    

    这应该默认使用底部麦克风

    【讨论】:

      【解决方案2】:

      如果你只需要音频,你应该使用AVAudioSession - https://developer.apple.com/reference/avfoundation/avaudiosession

      未测试您可以使用的示例代码:

      import AVFoundation
      .
      .
      private var session: AVAudioSession!
      private var input: AVAudioSessionPortDescription!
      .
      .
      .
      session = AVAudioSession.sharedInstance()
      do {
          try session.setCategory(AVAudioSessionCategoryRecord)
      
      // Fetch Built in Mic
      if let availableInputs = session.availableInputs {
          for inputSource in availableInputs {
              if inputSource.portType == AVAudioSessionPortBuiltInMic {
                  input = inputSource
                  break
              }
      }
      
      // Set preferred data source by location
      if let dataSources = input.dataSources {
          for dataSource in dataSources {
              if dataSource.location == AVAudioSessionLocationLower {
                  input.setPreferredDataSource(dataSource)
                  break 
              }
          }    
       }
      
       session.setPreferredInput(input)
       .
       .       
      } catch {
          ....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2011-09-05
        • 2017-08-25
        相关资源
        最近更新 更多