ERROR_CONNECTING_TO_SERVICE error occurred in YouTube development

Keywords: Android Mobile Fragment

Problem description

An initialization error occurred during android YouTube development, and the error prompt was ERROR_CONNECTING_TO_SERVICE. The effect of the error is as follows:

This error can be made on some machines, but not on others, such as Huawei machines. We know that when we use the API in YouTube's native jar, we have to install YouTube on the device before we can use it. This is because the use of API requires the use of YouTube software services. After testing on Huawei mobile phones, the device must first start YouTube, then the software developed by API can play YouTube videos (some devices do not need to start, as long as the device has YouTube on it).
Go to YouTube to find a solution, and give this sentence
There was an error connecting to the YouTube API service.

Suddenly silent. You have to let the user open YouTube once before every time you use the software, which is definitely not the case.

Solution

When the YouTube component is initialized, if it fails in the callback of the initialization result, the second way of playing video is adopted.

mPlayerView.initialize(YoutubeKey, new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    ...  
                    ...

                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                    //Initialization failed, try the second way

                    }
                }
            });

The second way to play is to use the YouTube Standalone Player class, the code is as follows:

private void playYoutubeByStandalone(String id){
        Intent intent = YouTubeStandalonePlayer.createVideoIntent(
                this, YoutubeKey, id, 0, true, false);
        if (intent != null) {
            if (canResolveIntent(intent)) {
                finish();
                startActivityForResult(intent, REQ_START_STANDALONE_PLAYER);
            } else {
                // Could not resolve the intent - must need to install or update the YouTube API service.
                YouTubeInitializationResult.SERVICE_MISSING
                        .getErrorDialog(this, REQ_RESOLVE_SERVICE_MISSING).show();
            }
        }



    }
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_START_STANDALONE_PLAYER && resultCode != RESULT_OK) {
            YouTubeInitializationResult errorReason =
                    YouTubeStandalonePlayer.getReturnedInitializationResult(data);
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog(this, 0).show();
            } else {
                String errorMessage =
                        String.format(getString(R.string.error_player), errorReason.toString());
                Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
        }
    }
 private boolean canResolveIntent(Intent intent) {
        List<ResolveInfo> resolveInfo = getPackageManager().queryIntentActivities(intent, 0);
        return resolveInfo != null && !resolveInfo.isEmpty();
    }

If you use YouTube Standalone Player to play YouTube videos, there will be no problem mentioned above. If you have used YouTube Standalone Player to play YouTube videos, YouTube services will be automatically activated, and then you can use YouTube Player View, YouTube Player Fragment to initialize the play, no problem.

Posted by erasam on Tue, 11 Dec 2018 17:57:06 -0800