ios - 在 ios 的 opentok 上更改相机

标签 ios objective-c opentok

我正在尝试更改 OpenTok 中发布者的摄像头。在 Android 中它非常简单,但我不明白如何在 ios 的 objective c 中做到这一点。

我试过了:

if (_publisher.cameraPosition == AVCaptureDevicePositionFront) 
{
    _publisher.cameraPosition = AVCaptureDevicePositionBack; // back camera

} else 
{
    _publisher.cameraPosition = AVCaptureDevicePositionFront; // front camera
}

我不得不说我是 Objective C(和 OpenTok)的初学者。

我该怎么办?

谢谢你:)

最佳答案

试试这个:

func setCameraPosition(_ position: AVCaptureDevicePosition) -> Bool {
        guard let preset = captureSession?.sessionPreset else {
            return false
        }

        let newVideoInput: AVCaptureDeviceInput? = {
            do {
                if position == AVCaptureDevicePosition.back {
                    return try AVCaptureDeviceInput.init(device: backFacingCamera())
                } else if position == AVCaptureDevicePosition.front {
                    return try AVCaptureDeviceInput.init(device: frontFacingCamera())
                } else {
                    return nil
                }
            } catch {
                return nil
            }
        }()

        guard let newInput = newVideoInput else {
            return false
        }

        var success = true

        captureQueue.sync {
            captureSession?.beginConfiguration()
            captureSession?.removeInput(videoInput)

            if captureSession?.canAddInput(newInput) ?? false {
                captureSession?.addInput(newInput)
                videoInput = newInput
            } else {
                success = false
                captureSession?.addInput(videoInput)
            }

            captureSession?.commitConfiguration()
        }

        if success {
            capturePreset = preset
        }

        return success
    }

    func toggleCameraPosition() -> Bool {
        guard hasMultipleCameras else {
            return false
        }

        if  videoInput?.device.position == .front {
            return setCameraPosition(.back)
        } else {
            return setCameraPosition(.front)
        }
    }

关于ios - 在 ios 的 opentok 上更改相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506435/

相关文章:

iphone - 显示 18 年前的 datepicker 并在 ios 中锁定 ui datepicker

objective-c - NSTask 终止处理程序仅在我发送 waitUntilExit 时运行

ios - 如何将核心图像过滤器添加到 OpenTok 视频?

objective-c - 我怎样才能更改此代码以使其不违反 MVC 原则?

android - AudioFocus 请求拒绝 OpenTok(Tokbox) 在相同添加的调用中,android 9 和 10

android - Titanium SDK 的 Opentok 模块同时支持 Android 和 IOS?

ios - 如何动态地将 UINavigationBar 中的 UISegmentControl 的宽度设置为最大宽度?

ios - 如何在 iOS 中按下主页按钮永久停止音频?

ios - 单击 float 按钮(ActionButton)直接转到另一个 View Controller (不生成 float 按钮选项)

objective-c - Xcode 是否有导航到文件/类的快捷方式?