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

前端 未结 3 1634
不知归路
不知归路 2020-12-10 05:41

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 no

相关标签:
3条回答
  • 2020-12-10 06:07

    This problem is due to browser restrictions, as explained in the doc: https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations

    I guess you could circumvent this restriction by simulating the user click ...

    0 讨论(0)
  • 2020-12-10 06:18

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 06:25

    I do not think this is possible at all. The same for iOS Safari (iPhone, iPad). User interaction is required to initiate video playback.

    0 讨论(0)
提交回复
热议问题