YouTube IFrame API 播放方法在某些 Android 平板电脑上触摸之前不起作用 [英] YouTube IFrame API play method doesn't work before touch on some Android tablets

查看:22
本文介绍了YouTube IFrame API 播放方法在某些 Android 平板电脑上触摸之前不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发 YouTube 播放器并使用 IFrame API.除了在我们的 Android 4.2.2 测试设备上,一切都运行良好.

We're developing a YouTube player and are using the IFrame API. Everything works really nice except on our Android 4.2.2 test devices.

仅在这些设备上(而不是在任何其他版本的 Android 上),有必要通过触摸视频视图手动"启动视频.在所有其他设备上,我们可以使用 YouTube 方法以编程方式开始播放视频.

Only on those devices (and not on any other version of Android), it is necessary to "manually" start the video by touching the video view. On all other devices, we can programatically start the video playing using the YouTube method.

以这种方式启动视频后,YouTube API 将按预期工作(即播放、暂停、停止方法都按预期以编程方式工作).

Once the video has been started in this way, the YouTube API works as expected (i.e. play, pause, stop methods all work programatically as expected).

这是我们代码的精髓:

var player;
function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    height: '390',
    width: '640',
    videoId: 'C0DPdy98e4c',
    playerVars: {
      controls: 0,
      showinfo: 0,
      modestbranding: 1
    }
  });
}

function playVideo() {
  player.playVideo();
}

如果我们在用户手动"播放视频之前尝试以编程方式播放视频(在 Android 4.2.2 设备上),视频会开始缓冲然后失败.失败后,视频视图会变黑并显示一个独特的图案,如图所示视频视图的左上角:

If we attempt to programatically play the video before a user has "manually" started the video (on Android 4.2.2 devices), the video begins buffering and then fails. Upon failure, the video view goes black and displays a distinct pattern, seen in the top left of the video view in image here:

有没有其他人遇到过这个问题?有人对如何处理有任何建议吗?

Has any anyone else experienced this issue? Does anyone have any suggestions about what to do about it?

推荐答案

这是一个已知问题,您有两种可能的解决方案:

This is a known problem for which you have two possible solutions:

1) 如果您可以定位 APi >= 17,则可以依赖新的 WebView 和新的 WebSettings api 方法 setMediaPlaybackRequiresUserGesture()

1) If you can target APi >= 17 you can rely on the new WebView and the new WebSettings api method setMediaPlaybackRequiresUserGesture()

WebSettings settings = webview.getSettings();
settings.setMediaPlaybackRequiresUserGesture(false);

2) 如果你的目标 api 是 <17 比你必须在正确的时间模拟用户在 WebView 上的点击(比如在页面加载之后和发送播放命令之前):

2) If your target api is < 17 than you have to simulate an user's tap on the WebView at the right time (like after the page is loaded and before sending the play command):

private void emulateClick(final WebView webview) {
    long delta = 100;
    long downTime = SystemClock.uptimeMillis();
    float x = webview.getLeft() + webview.getWidth()/2; //in the middle of the webview
    float y = webview.getTop() + webview.getHeight()/2;

    final MotionEvent motionEvent = MotionEvent.obtain( downTime, downTime + delta, MotionEvent.ACTION_DOWN, x, y, 0 );
    final MotionEvent motionEvent2 = MotionEvent.obtain( downTime + delta + 1, downTime + delta * 2, MotionEvent.ACTION_UP, x, y, 0 );

    Runnable tapdown = new Runnable() {
        @Override
        public void run() {
            if (webview != null) {
                webview.dispatchTouchEvent(motionEvent);
            }
        }
    };

    Runnable tapup = new Runnable() {
        @Override
        public void run() {
            if (webview != null) {
                webview.dispatchTouchEvent(motionEvent2);
            }
        }
    };

    int toWait = 0;
    int delay = 100;
    webview.postDelayed(tapdown, delay);
    delay += 100;
    webview.postDelayed(tapup, delay);
}

这篇关于YouTube IFrame API 播放方法在某些 Android 平板电脑上触摸之前不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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