[Swift🕊️]I am implementing the app with external microphone

iPhone

As usual, I asked for ChatGPT on how to be able to use external microphone in iOS. And as usual, he/she answered with a smile (actually I don't know). But he is a littel bit old. I modified some codes.

me
I want to apply to use Zoom iQ7 microphone.

But app cannot use it.I am developing by iOS. What should I add extra code ?

ChatGPT
To use the Zoom iQ7 microphone in your iOS app, you need to ensure that your app has the necessary permissions to access the microphone and that you are correctly configuring your audio session. Here are the general steps you can follow:

  1. Request Microphone Permission:
    Ensure that your app has the necessary permissions to access the device's microphone. In your app's Info.plist, add the following key:

info.plist

<key>NSMicrophoneUsageDescription</key>
<string>We need access to your microphone to record audio.</string>

This is a user-facing description that explains why your app requires microphone access. Make sure to provide a clear and concise message.

  1. Configure Audio Session:
    Configure your app's audio session appropriately. You can do this in your AppDelegate or wherever your audio configuration is set up. For example:

swift

import AVFoundation

func setupAudioSession() {
    do {
        try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print("Failed to configure audio session: \(error)")
    }
}

Ensure that you call setupAudioSession before using the microphone.

  1. Check Zoom iQ7 Connection:
    Make sure that the Zoom iQ7 microphone is connected and recognized by the device. You can use the AVCaptureDevice API to check for available audio input devices:

swift

import AVFoundation

      let devices: AVCaptureDevice.DiscoverySession
      if #available(iOS 17.0, *) {
        devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.microphone], mediaType: .audio, position: .unspecified)
      } else {
        devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: .audio, position: .unspecified)
      }
      print("Audio Device: \(devices.devices)")

Check if the Zoom iQ7 appears in the list of available audio devices.

  1. Select Zoom iQ7 as Input:
    Once you have confirmed that the Zoom iQ7 is available, select it as the preferred input device for your audio session:

swift

import AVFoundation

func selectAudioDevice() {
    if let zoomIQ7 = AVCaptureDevice.devices(for: .audio).first(where: { $0.localizedName == "Zoom iQ7" }) {
        do {
            try AVAudioSession.sharedInstance().setPreferredInput(zoomIQ7)
        } catch {
            print("Failed to set preferred input: \(error)")
        }
    }
}
  1. Test Audio Recording:
    Finally, test audio recording using the Zoom iQ7. You can use AVAudioRecorder to record audio:

swift

import AVFoundation

func startRecording() {
    let url = // specify the URL where you want to save the recording
    let settings: [String: Any] = [
        AVFormatIDKey: kAudioFormatAppleLossless,
        AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
        AVEncoderBitRateKey: 320000,
        AVNumberOfChannelsKey: 2,
        AVSampleRateKey: 44100.0
    ]

    do {
        let audioRecorder = try AVAudioRecorder(url: url, settings: settings)
        audioRecorder.record()
    } catch {
        print("Failed to start recording: \(error)")
    }
}

Make sure to handle errors appropriately and provide a good user experience.

Note: The device name "Zoom iQ7" used in the examples is illustrative. Replace it with the actual name or identifier of the Zoom iQ7 device.

These steps should help you integrate and use the Zoom iQ7 microphone in your iOS app.

me
Thank you so much.

(please do not foget appliciation.)

コメント

タイトルとURLをコピーしました