It seems to be a YouTube bug, view YouTubeAndroidPlayerAPI can't play some videos for details that I collected.
the only passable workaround at this time appears to be open the video in the YouTube official app with an intent or replace the player fragment with a WebView. I preferred this second solution.
The standard WebView is very limited and will not show the button to bring the video fullscreen. You need to create a class that extend WebChromeClient:
public class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView = (LinearLayout) findViewById(R.id.scheda_video_activity);
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(SchedaVideoActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
@Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
mCustomView.setVisibility(View.GONE);
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
and then initialize the WebView:
WebView myWebView = (WebView)findViewById(R.id.webview);
MyWebChromeClient mWebChromeClient = new MyWebChromeClient();
myWebView.setWebChromeClient(mWebChromeClient);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Finally, load the video in the WebView:
myWebView.loadUrl("https://www.youtube.com/embed/"+youtube_id);
If you want to adapt the WebView dimensions to the YouTube player you can do this:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
myWebView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)Math.round(size.x/1.77)));