我正在尝试在我的测试应用程序上使用 youtube 视频,我找到了 youtube-ios-player-helper。不幸的是,我找不到任何将它与 SwiftUI 一起使用的示例。谁能帮我解决一下?
最佳答案
由于 YTPlayerView
是一个 UIView
,您需要将它包装在 UIViewRepresentable 中才能在 SwiftUI 中使用它:
import YouTubeiOSPlayerHelper
struct YTWrapper : UIViewRepresentable {
var videoID : String
func makeUIView(context: Context) -> YTPlayerView {
let playerView = YTPlayerView()
playerView.load(withVideoId: videoID)
return playerView
}
func updateUIView(_ uiView: YTPlayerView, context: Context) {
//
}
}
struct ContentView: View {
var body: some View {
YTWrapper(videoID: "jQtP1dD6jQ0")
}
}
请注意,您可能需要根据需要对视频的加载位置做出不同的决定(makeUIView
与 updateUIView
)。
关于swiftui - 如何在 SwiftUI 项目中使用 youtube-ios-player-helper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65998718/