【发布时间】:2023-02-24 18:51:16
【问题描述】:
如何使用 Swift/OC 以编程方式在 macOS 上获取输入级别。
为了获得“输入音量”,这里有一个解决方案。Input volume solution 但我找不到任何方法来获得 macOS 上的输入级别。
【问题讨论】:
-
您链接的问题是对于 macOS。你可能是说你想要一个 Swift 中的解决方案?
标签: swift objective-c macos
如何使用 Swift/OC 以编程方式在 macOS 上获取输入级别。
为了获得“输入音量”,这里有一个解决方案。Input volume solution 但我找不到任何方法来获得 macOS 上的输入级别。
【问题讨论】:
标签: swift objective-c macos
要使用 Swift/Objective-C 以编程方式获取 macOS 上的输入级别,您可以使用AVCapture设备来自 AVFoundation 框架的类。
这是 Swift 中的一个示例:
import AVFoundation
// Get the default audio input device
guard let audioDevice = AVCaptureDevice.default(for: .audio) else {
print("No audio device found")
return
}
// Get the audio input level
do {
try audioDevice.lockForConfiguration()
let inputLevel = audioDevice.inputVolume
audioDevice.unlockForConfiguration()
print("Input level: (inputLevel)")
} catch {
print("Error getting input level: (error.localizedDescription)")
}
这是 Objective-C 中的等效代码:
#import <AVFoundation/AVFoundation.h>
// Get the default audio input device
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
// Get the audio input level
NSError *error = nil;
if ([audioDevice lockForConfiguration:&error]) {
float inputLevel = audioDevice.inputVolume;
[audioDevice unlockForConfiguration];
NSLog(@"Input level: %f", inputLevel);
} else {
NSLog(@"Error getting input level: %@", error.localizedDescription);
}
笔记 :在这两个示例中,我们首先使用默认(对于:)或者默认设备与媒体类型:方法,然后使用输入音量属性来获取输入电平。我们还需要锁定设备以使用锁定配置()访问前的方法输入音量属性,并使用解锁配置()完成后的方法。
【讨论】: