Chatgpt初体验--macos开发切换麦克风

不得不说,目前还是替代不了程序员

  • 有点答非所问

问:Use swift to switch macos microphone (使用swift 切换macos麦克风)

答: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

图片.png

真实解决还是靠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;
    }

切换麦克风

 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)
            }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容