Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

javascript - YouTube IFrame API play method doesn't work before touch on some Android tablets

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.

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.

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).

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();
}

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:

YouTube API failure on Android 4.2.2 here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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) 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...