如何在Android中使用Kotlin和jetpack compose实现YouTube IFrame Player API?

brc7rcf0  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(180)

YouTube Android Player API SDK已完全弃用。YouTube不再支持或维护它。因此需要在Android中实现YouTube IFrame Player API。
播放YouTube视频时,显示“初始化YouTube播放器时发生错误”消息。

vwoqyblh

vwoqyblh1#

根据developer.google文档https://developers.google.com/youtube/iframe_api_reference,我使用Kotlin和jetpack compose使用IFrame YouTube player for android创建了一个示例应用程序。
使用下面的依赖项

实现“androidx.webkit:webkit:1.4.0”

在Manifest.xml中给予Internet权限

使用权限android:name=“android.permission.INTERNET”

创建合成函数

@Composable
fun YoutubeVideoPlayer(videoId: String) {
    val webView = WebView(LocalContext.current).apply {
        settings.javaScriptEnabled = true
        settings.loadWithOverviewMode = true
        settings.useWideViewPort = true
        webViewClient = WebViewClient()
    }

    val htmlData = getHTMLData(videoId)

    Column(Modifier.fillMaxSize()) {

        AndroidView(factory = { webView }) { view ->
            view.loadDataWithBaseURL(
                "https://www.youtube.com",
                htmlData,
                "text/html",
                "UTF-8",
                null
            )
        }
        Button(onClick = {
            webView.loadUrl("javascript:playVideo();")
        }) {
            Text(text = "Play Video")
        }
        Button(onClick = {
            webView.loadUrl("javascript:pauseVideo();")
        }) {
            Text(text = "Pause Video")
        }
        Button(onClick = {
            webView.loadUrl("javascript:seekTo(60);")
        }) {
            Text(text = "Seek Video")
        }
    }
}

使用getHTMLData函数加载包含IFrame的HTML页面

fun getHTMLData(videoId: String): String {
   return """
        <html>
        
            <body style="margin:0px;padding:0px;">
               <div id="player"></div>
                <script>
                    var player;
                    function onYouTubeIframeAPIReady() {
                        player = new YT.Player('player', {
                            height: '450',
                            width: '650',
                            videoId: '$videoId',
                            playerVars: {
                                'playsinline': 1
                            },
                            events: {
                                'onReady': onPlayerReady
                            }
                        });
                    }
                    function onPlayerReady(event) {
                     player.playVideo();
                        // Player is ready
                    }
                    function seekTo(time) {
                        player.seekTo(time, true);
                    }
                      function playVideo() {
                        player.playVideo();
                    }
                    function pauseVideo() {
                        player.pauseVideo();
                    }
                </script>
                <script src="https://www.youtube.com/iframe_api"></script>
            </body>
        </html>
    """.trimIndent()
}

在主活动中调用编写函数

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column {
                YoutubeVideoPlayer(videoId = "bHQqvYy5KYo")
            }
        }
    }
}

相关问题