有许多软件包pub.dev
利用 YouTube iFrame API 来播放 YT 视频内联。但是我找不到使用本机播放器的包,所以我决定创建自己的包。但我正在努力让它在 Android 上运行。通过io.flutter.plugin.platform.PlatformView
在 Android 上实现并添加com.google.android.youtube.player.YouTubePlayerView
到视图中:
internal class YTNativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView, YouTubePlayer.OnInitializedListener {
private val ytPlayer = YouTubePlayerView(context)
private val apiKey = "<your-api-key>"
init {
ytPlayer.initialize(apiKey, this)
}
override fun getView(): View = ytPlayer
override fun dispose() {}
override fun onInitializationSuccess(provider: YouTubePlayer.Provider, youTubePlayer: YouTubePlayer, b: Boolean) {
youTubePlayer.loadVideo("QH2-TGUlwu4")
youTubePlayer.play()
}
override fun onInitializationFailure(provider: YouTubePlayer.Provider, youTubeInitializationResult: YouTubeInitializationResult) {}
}
抛出以下错误:
Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
然后我尝试使用com.google.android.youtube.player.YouTubePlayerFragment
,但由于缺少getFragmentManager()
. 有什么想法、解决方案或提示吗?