首页 > 解决方案 > 如何使用 swiftUI 创建视频通话功能?

问题描述

我正在使用 SwiftUI 创建一个消息传递应用程序,并且我想向其中添加视频通话功能。我使用 SkyWay webRTC API ( https://webrtc.ecl.ntt.com/en/ ) 来实现这一点,我可以构建一个用 swift 代码编写的示例项目。现在我正在尝试将本地流与 SKWVideo 视图链接并将其包装到 UIViewRepresentable 中。但是我遇到了以下错误消息。

import SwiftUI
import SkyWay
import UIKit

struct ContentView: View {
    @State var video = SKWVideo()

    var body: some View {
        VideoView(localStreamView: $video)
    }
}

struct VideoView: UIViewRepresentable {
    @Binding var localStreamView: SKWVideo

    func makeUIView(context: Context) -> SKWVideo {
        let option: SKWPeerOption = SKWPeerOption.init()
        option.key = "xxxx"
        option.domain = "localhost"
        let peer = SKWPeer(options: option)
        SKWNavigator.initialize(peer!)
        let constraints: SKWMediaConstraints = SKWMediaConstraints()
        let localStream = SKWNavigator.getUserMedia(constraints)
        localStream?.addVideoRenderer(localStreamView, track: 0)
        return localStreamView
    }

    func updateUIView(_ uiView: SKWVideo, context: Context) {
        //
    }
}

我收到了这个错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'

我在 iPhone 7 真实设备上运行它,并编写了 plist 设置。我现在完全不知道。请帮我..

iOS 13.0 xcode 11.0

标签: webrtcswiftui

解决方案


推荐阅读