I am developing an app that takes youtube video id's from the backend and it should play the videos from the queue automatically without the intervention of the user. But these are the challenges that I Faced with different approaches.
Using Youtube API:- This is not supported by FireTV and my App is not approved in the Amazon store.
Using Webview:-
When I use the video URL as "https://www.youtube.com/watch?v=ajMpIN6BvQE" youtube videos are started playing automatically. But I am seeing the recommendations to the video and unable to get rid of these recommendations automatically
When I use the video URL as "https://www.youtube-nocookie.com/embed/ajMpIN6BvQE" it stopped showing the recommendations but the challenge here is I am unable to play the videos automatically. It is forcing the user to click on the play button explicitly. bellow is the code I have used
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class WebViewActivity extends Activity {
WebView webView;
WebChromeClient.CustomViewCallback customViewCallback;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
ChromeClient chromeClient = new ChromeClient();
webView.setWebChromeClient(chromeClient);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
// webView.loadUrl("https://www.youtube-nocookie.com/embed/ajMpIN6BvQE");
webView.loadUrl("https://www.youtube.com/watch?v=ajMpIN6BvQE");
webSettings.setMediaPlaybackRequiresUserGesture(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
@Override
public void onPause() {
webView.onPause();
super.onPause();
}
@Override
public void onDestroy() {
webView.destroy();
super.onDestroy();
}
@Override
public void onStop() {
super.onStop();
}
@Override
protected void onRestart() {
super.onRestart();
}
public void onBackPressed() {
webView.stopLoading();
finish();
}
@Override
protected void onStart() {
super.onStart();
}
public class ChromeClient extends WebChromeClient {
protected FrameLayout mFullscreenContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
ChromeClient() {
}
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
@Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
@Override
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}}
I am able to achieve a solution with https://developers.google.com/youtube/iframe_api_reference. Added a Javascript code and enabled the interactions between Java script to Native android.