Joseph Ofem
Joseph Ofem

Reputation: 384

Webview video (iframe) not playing except sound is muted

I am trying to play a HTML iframe video but the video refuses to play unless i mute the sound.This is my the web view method.

private void playVideoOnWebView(String videoUrl) {
        videoWebViewPlayer = findViewById(R.id.videoPlayerWebView);           
        videoWebViewPlayer.setVisibility(VISIBLE);
        videoWebViewPlayer.getSettings().setJavaScriptEnabled(true);    
        videoWebViewPlayer.getSettings().getAllowContentAccess();
        videoWebViewPlayer.getSettings().setDomStorageEnabled(true);
        videoWebViewPlayer.getSettings().setAppCacheEnabled(true);videoWebViewPlayer.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
        videoWebViewPlayer.getSettings().setDatabaseEnabled(true);
        videoWebViewPlayer.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
        videoWebViewPlayer.getSettings().setPluginState(WebSettings.PluginState.ON);
        videoWebViewPlayer.getSettings().setDomStorageEnabled(true);
        videoWebViewPlayer.setWebChromeClient(new WebChromeClient());
        videoWebViewPlayer.loadUrl(videoUrl);
    }

Thanks...

Upvotes: 3

Views: 1068

Answers (1)

jeprubio
jeprubio

Reputation: 18022

As you say that if the video is muted it plays without a problem it seems to me you are trying to autoplay a video without following Chrome's autoplay policies:

  • Muted autoplay is always allowed.
  • Autoplay with sound is allowed if:
    • User has interacted with the domain (click, tap, etc.).
    • On desktop, the user's Media Engagement Index threshold has been crossed, meaning the user has previously played video with sound.
    • The user has added the site to their home screen on mobile or installed the PWA on desktop.
  • Top frames can delegate autoplay permission to their iframes to allow autoplay with sound.

The last one might be the cause, to allow autoplay from iframes (when the video is not mute) the allow attribute must be set to autoplay:

<iframe src="https://cross-origin.com/myvideo.html" allow="autoplay">

I also struggled autoplay restrictions some time ago.

Upvotes: 1

Related Questions