如何检查webview YouTube视频播放器是否已启动 [英] How to check if webview YouTube video player was started

查看:83
本文介绍了如何检查webview YouTube视频播放器是否已启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我有一个Webview,该Webview中有一个嵌入式YouTube视频.我的应用有本地AdMob横幅.

In my Android app, I have a webview where there is an embedded YouTube video inside the webview. My app has a native AdMob banner.

我想在用户播放视频时从应用程序中隐藏本机admob横幅,因此横幅在视频播放时不会显示,然后在视频停止播放时再次显示广告

I'd like to hide the native admob banner from the app when user plays the video, so the banner does not show while the video is playing and then show the ads again when the video stops playing

问题是我不知道如何检查视频是开始还是停止.

The issue is that I do not know how to check if the video was started or stopped.

有什么想法可以做到吗?非常感谢.

Any idea how this can be done? Thanks much.

推荐答案

webview和native之间的通信是通过JavascriptInterface完成的.YouTube以类似的方式构建了它的API.您可以使用以下代码实现所需的功能.

The communication between webview and native are done through JavascriptInterface. YouTube has built it's API in a similar fashion. you can use the below code to achieve what you want.

要通过YouTube视频实现播放/暂停功能,您可以使用 YouTube JavaScript Player API

To achieve the play/pause functionality via Youtube video you can use YouTube JavaScript Player API.

示例:

<div id="video-placeholder"></div>

<script src="https://www.youtube.com/iframe_api"></script>

API完全加载后,它将查找您应定义的名为 onYouTubeIframeAPIReady()的全局函数.

When the API is fully loaded, it looks for a global function called onYouTubeIframeAPIReady() which you should define.

您的代码应如下所示

var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('video-placeholder', {
        width: 600,
        height: 400,
        videoId: 'Xa0Q0J5tOP0',
        playerVars: {
            color: 'white',
            playlist: 'taJ60kskkns,FG0fTKAqZ5g'
        },
        events: {
            onReady: initialize
        }
    });
}

只需单击两个按钮,然后单击单击即可调用所需的方法.

Just make two buttons and call the needed method on click.

$('#play').on('click', function () {
    player.playVideo();
});

$('#pause').on('click', function () {
    player.pauseVideo();
});

您可以使用 JavascriptInterface 在本机java/中获取回调WebView中的kotlin代码.

You can use JavascriptInterface to get the callback inside the native java/kotlin code from WebView.

更多详细信息

  1. Youtube javascript播放器API示例

JavaScript接口(示例)

这篇关于如何检查webview YouTube视频播放器是否已启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆