7

Using the YouTube Android Player API sample code (version 1.2.2), if I change any of the video IDs over to one I have uploaded (e.g. "QVikru_w2hQ" or "u1n6E81rm80"), the thumbnail loads in the player, but on clicking play, the player goes to the onError function with YouTubePlayer.ErrorReason.INTERNAL_ERROR. A message is displayed saying "There was a problem while playing. Tap to retry". Playing these videos on the YouTube website or our iOS app does not have a problem. The original sample videos play fine with my developer key.

I am experiencing this issue on multiple devices, including my Nexus 5X (android 6.0.1) with YouTube app version currently at 11.13.56.

Has anyone else experienced a similar issue?

I have also posted a potential bug report on google data api issue tracker.

Jon G
  • 1,656
  • 3
  • 16
  • 42
  • is this working on any device? – Vivek Mishra May 04 '16 at 10:36
  • @VivekMishra : Galaxy S4, 5.0.1, 11.10.60 -- No. HTC One M8, 6.0, 11.10.60 -- No. HTC Desire 610, 4.4.2, 11.13.56 -- No. I've not found one yet. – Jon G May 04 '16 at 12:40
  • I also tried the sample code and it certainly not works with video id that you provided but it is working with many random video id that I picked from you tube and it is working fine. I think it may be related to accessibility issue – Vivek Mishra May 04 '16 at 13:07
  • @JonG : same issue here. I have +900 videos hosted on various youtube channels, the majority of them are not playing anymore on many android devices. While they all play fine on iOS. Strange thing is that some videos work and some others don't. All this happened a few days ago. Have you find a solution ? – batistomorrow May 04 '16 at 13:42
  • @jonG same issue here. New videos are not playing but videos uploaded earlier are playing fine. – dinwal May 05 '16 at 07:41
  • @jonG : same issue here. Random videos are working and sometimes videos are throwing error albeit were working earlier. – Mohammed Vaseem Siddiqui May 05 '16 at 19:08
  • @Jon G facing exactly same situation, have you got any solution? – Khurram Shehzad May 19 '16 at 19:04
  • @khurramengr Either of the answers from batistomorrow or frabbe are valid work-arounds. Equally you could try getting access to the underlying YouTube mp4 files, but that probably breaks there terms of service. No word on a fix from YouTube themselves yet. – Jon G May 20 '16 at 08:49

2 Answers2

2

The WebView seems to be a good solution. Because I was in a rush and needed a quick fix, I used the source code from this github project :

https://github.com/theScrabi/NewPipe

This code does not rely on the Youtube API. It is pure web parsing. All my videos are working fine with the solution, as fast as before.

The whole project is a functional app, but you can dive inside and take what you need. I used the fragment which embeds the video thumbnail and the fullscreen player activity.

Kudos to the author of the project.

batistomorrow
  • 674
  • 1
  • 6
  • 13
0

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)));
Community
  • 1
  • 1
frabbe
  • 111
  • 1
  • 6
  • Did you get autoplay to work with this method? I haven't managed to yet. `setMediaPlaybackRequiresUserGesture(false)` doesn't seem to help. – Jon G May 16 '16 at 14:12
  • No, you can't autoplay video with this simple code... the user must tap the "play" button. – frabbe May 16 '16 at 15:38