AVCaptureDevice.DiscoverySession().devices 返回任何设备。

huangapple go评论68阅读模式
英文:

AVCaptureDevice.DiscoverySession().devices not returning any devices

问题

以下是您提供的Swift代码的翻译部分:

let devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone, .externalUnknown], mediaType: .audio, position: .unspecified).devices
print("DEVICES: \(devices.count)")

运行该代码时,它会在控制台上打印“DEVICES: 0”。我在运行此代码时正在使用Zoom,并且在菜单中看到橙色点,所以我知道麦克风正在使用。

我缺少什么?

编辑:我正在构建一个播放声音的应用程序,但如果麦克风正在使用,我不希望播放声音。例如,如果用户正在Zoom会议中,播放声音会对会议产生干扰。所以我在思考如果我能确定其他应用程序正在使用麦克风(我不关心用途,也不需要访问麦克风本身),那么我可以暂时禁用播放声音文件。

英文:

I have the following Swift code:

let devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone, .externalUnknown], mediaType: .audio, position: .unspecified).devices
print("DEVICES: \(devices.count)")

However, when run it prints "DEVICES: 0" to the console. I'm running Zoom when I run this code, and I see the orange dot in the menu, so I know the mic is in use.

What am I missing?

EDIT: I'm building an app that plays a sound but, I don't want to play that sound if the mic is hot. For example; if the user is on a Zoom call then playing the sound would be distracting to the meeting. So I'm thinking if I can determine that some other app is using the mic (I don't care what for and don't need to access the mic itself) then I can temporarily disable playing the sound file.

答案1

得分: 1

First thing, you have to make sure you have (or the user accepted) the permissions to access the microphone. You can check and request microphone permissions using the AVAudioSession class, also try to verify the permissions you have granted in the General > Your application section.

Another thing you can check if the microphone is actually available for use by your application. Sometimes there are applications, like Zoom in your case, might be using the microphone exclusively, preventing your code from accessing it.

If you can expand your code, you can show how you are using the AVAudioSession class. You can use the requestRecordPermission() method to request permission.

Also, in order to verify if there is another application who is taking exclusive control over the microphone, you might use:

func isMicrophoneInUse(by application: String) -> Bool {
  let audioSession = AVAudioSession.sharedInstance()
  let category = AVAudioSession.Category.record
  let options = AVAudioSession.CategoryOptions.duckOthers

  do {
    try audioSession.setCategory(category, options: options)
    let activeMicrophone = audioSession.currentRoute.inputs.first
    return activeMicrophone?.owningApplication == application
  } catch {
    return false
  }
}

or in a more complicated way, you can use the AVAudioSession.routeChangeNotification notification to receive updates when the audio route changes, which may indicate that another application has taken control of the microphone.

Hope it helps.

英文:

First thing, you have to make sure you have (or the user accepted) the permissions to access the microphone. You can check and request microphone permissions using the AVAudioSession class, also try to verify the permissions you have granted in the General > Your application section.
Another thing you can check if the microphone is actually available for use by your application. Sometimes there are applications, like Zoom in your case, might be using the microphone exclusively, preventing your code from accessing it.

If you can expand your code, you can show how you are using the AVAudioSession class. You can use the requestRecordPermission() method to request permission.

Also, in order to verify if there is another application who is taking exclusive control over the microphone, you might use:

func isMicrophoneInUse(by application: String) -> Bool {
  let audioSession = AVAudioSession.sharedInstance()
  let category = AVAudioSession.Category.record
  let options = AVAudioSession.CategoryOptions.duckOthers

  do {
    try audioSession.setCategory(category, options: options)
    let activeMicrophone = audioSession.currentRoute.inputs.first
    return activeMicrophone?.owningApplication == application
  } catch {
    return false
  }
}

or in a more complicated way, you can use the AVAudioSession.routeChangeNotification notification to receive updates when the audio route changes, which may indicate that another application has taken control of the microphone.

Hope it helps

huangapple
  • 本文由 发表于 2023年5月18日 10:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定