ube的IFrame的API播放方法没有触及一些Android平

ube的IFrame的API播放方法没有触及一些Android平

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

问题描述

我们正在开发一个YouTube播放器,并使用了IFrame的API。一切工作真的很好,除了对我们的Andr​​oid 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.

只有在这些设备(而不是任何其他版本的Andr​​oid),有必要为手动通过触摸视频视图启动视频。在所有其他设备,我们可以使用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).

下面是我们的code精华:

Here's the essence of our code:

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比你要模拟在web视图的用户的敲击在正确的时间(在页面加载后喜欢和发送播放命令前):

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平板电脑前工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:12