不得不說循头,目前還是替代不了程序員
- 有點答非所問
問:Use swift to switch macos microphone (使用swift 切換macos麥克風(fēng))
答:You can use Swift programming language to switch the microphone on macOS by utilizing the AVFoundation framework. Here's a sample code snippet that demonstrates how to switch between the input devices on macOS:
import AVFoundation
func switchInputDevice() {
let session = AVAudioSession.sharedInstance()
do {
let inputs = session.availableInputs
for input in inputs! {
if input.portType == AVAudioSession.Port.builtInMic {
try session.setPreferredInput(input)
break
}
}
} catch {
print("Error switching input device: \(error)")
}
}
In this code snippet, we import the AVFoundation framework and define a function named "switchInputDevice". This function gets the shared audio session and retrieves all available input devices. It then iterates through the input devices and checks if the input device is a built-in microphone. If it finds a built-in microphone, it sets it as the preferred input device using the setPreferredInput method of the AVAudioSession class.
You can call this function whenever you want to switch between input devices, such as when you want to switch to a different microphone. Note that this code snippet only demonstrates how to switch to a built-in microphone. If you want to switch to a different type of input device, you will need to modify the code accordingl
真實解決還是靠google了
切換攝像頭
var devices: [AVCaptureDevice] = []
if #available(macOS 10.15, *) {
let cameras = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera, .externalUnknown], mediaType: .video, position: .unspecified)
devices = cameras.devices
} else {
devices = AVCaptureDevice.devices(for: .video)
}
var cameraModel = [RTMenuModel]()
for device in devices {
let model = RTMenuModel()
model.uniqueID = device.uniqueID
model.deviceId = device.uniqueID
model.name = device.localizedName
cameraModel.append(model)
}
- 使用攝像頭處镶奉,返回使用切換的攝像頭勋磕。
func captureDevice(atPosition position: AVCaptureDevice.Position, uniqueID: String?) -> AVCaptureDevice? {
if #available(macOS 10.15, *) {
let deviceSession = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera, .externalUnknown], mediaType: .video, position: position)
let devices = deviceSession.devices
for device in devices {
if uniqueID == nil {
return device
}else{
if device.uniqueID == uniqueID {
return device
}
}
}
}else{
let devices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in devices {
if selectCameraUniqueID == nil {
return device
}else{
if device.uniqueID == selectCameraUniqueID {
return device
}
}
}
}
return nil;
}
切換麥克風(fēng)
var devices: [AVCaptureDevice] = []
if #available(macOS 10.15, *) {
let audios = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInMicrophone], mediaType: .audio, position: .unspecified)
devices = audios.devices
} else {
devices = AVCaptureDevice.devices(for: .audio)
}
var audioModel = [RTMenuModel]()
for device in devices {
let model = RTMenuModel()
model.uniqueID = device.uniqueID
model.deviceId = String(describing: device.value(forKey: "_connectionID")!)
model.name = device.localizedName
audioModel.append(model)
}