Example #1
0
        // Internal methods

        private void InitializePlayer()
        {
            if (player == null)
            {
                Intent            intent = Intent;
                string            action = intent.Action;
                android.Net.Uri[] uris;
                string[]          extensions;

                if (ACTION_VIEW.Equals(action))
                {
                    uris       = new android.Net.Uri[] { intent.Data };
                    extensions = new string[] { intent.GetStringExtra(EXTENSION_EXTRA) };
                }
                else if (ACTION_VIEW_LIST.Equals(action))
                {
                    string[] uristrings = intent.GetStringArrayExtra(URI_LIST_EXTRA);
                    uris = new android.Net.Uri[uristrings.Length];
                    for (int i = 0; i < uristrings.Length; i++)
                    {
                        uris[i] = android.Net.Uri.Parse(uristrings[i]);
                    }
                    extensions = intent.GetStringArrayExtra(EXTENSION_LIST_EXTRA);
                    if (extensions == null)
                    {
                        extensions = new string[uristrings.Length];
                    }
                }
                else
                {
                    ShowToast(GetString(Resource.String.unexpected_intent_action, action));
                    Finish();
                    return;
                }
                if (Utils.MaybeRequestReadExternalStoragePermission(this, uris))
                {
                    // The player will be reinitialized if the permission is granted.
                    return;
                }

                DefaultDrmSessionManager drmSessionManager = null;
                if (intent.HasExtra(DRM_SCHEME_EXTRA) || intent.HasExtra(DRM_SCHEME_UUID_EXTRA))
                {
                    string   drmLicenseUrl             = intent.GetStringExtra(DRM_LICENSE_URL_EXTRA);
                    string[] keyRequestPropertiesArray =
                        intent.GetStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES_EXTRA);
                    bool multiSession  = intent.GetBooleanExtra(DRM_MULTI_SESSION_EXTRA, false);
                    int  errorstringId = Resource.String.error_drm_unknown;
                    if (Utils.SdkInt < 18)
                    {
                        errorstringId = Resource.String.error_drm_not_supported;
                    }
                    else
                    {
                        try
                        {
                            string drmSchemeExtra = intent.HasExtra(DRM_SCHEME_EXTRA) ? DRM_SCHEME_EXTRA
                                : DRM_SCHEME_UUID_EXTRA;
                            UUID drmSchemeUuid = Utils.GetDrmUuid(intent.GetStringExtra(drmSchemeExtra));
                            if (drmSchemeUuid == null)
                            {
                                errorstringId = Resource.String.error_drm_unsupported_scheme;
                            }
                            else
                            {
                                drmSessionManager =
                                    BuildDrmSessionManagerV18(
                                        drmSchemeUuid, drmLicenseUrl, keyRequestPropertiesArray, multiSession);
                            }
                        }
                        catch (UnsupportedDrmException e)
                        {
                            errorstringId = e.Reason == UnsupportedDrmException.ReasonUnsupportedScheme
                                ? Resource.String.error_drm_unsupported_scheme : Resource.String.error_drm_unknown;
                        }
                    }
                    if (drmSessionManager == null)
                    {
                        ShowToast(errorstringId);
                        Finish();
                        return;
                    }
                }

                ITrackSelectionFactory trackSelectionFactory;
                string abrAlgorithm = intent.GetStringExtra(ABR_ALGORITHM_EXTRA);
                if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.Equals(abrAlgorithm))
                {
                    trackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
                }
                else if (ABR_ALGORITHM_RANDOM.Equals(abrAlgorithm))
                {
                    trackSelectionFactory = new RandomTrackSelection.Factory();
                }
                else
                {
                    ShowToast(Resource.String.error_unrecognized_abr_algorithm);
                    Finish();
                    return;
                }

                bool preferExtensionDecoders =
                    intent.GetBooleanExtra(PREFER_EXTENSION_DECODERS_EXTRA, false);
                int extensionRendererMode =
                    ((DemoApplication)Application).UseExtensionRenderers()
                        ? (preferExtensionDecoders ? DefaultRenderersFactory.ExtensionRendererModePrefer
                        : DefaultRenderersFactory.ExtensionRendererModeOn)
                        : DefaultRenderersFactory.ExtensionRendererModeOff;
                DefaultRenderersFactory renderersFactory =
                    new DefaultRenderersFactory(this, extensionRendererMode);

                trackSelector = new DefaultTrackSelector(trackSelectionFactory);
                trackSelector.SetParameters(trackSelectorParameters);
                lastSeenTrackGroupArray = null;

                player = ExoPlayerFactory.NewSimpleInstance(renderersFactory, trackSelector, drmSessionManager);

                eventLogger = new EventLogger(trackSelector);

                player.AddListener(new PlayerEventListener(this));
                player.PlayWhenReady = startAutoPlay;

                player.AddListener(eventLogger);

                // Cannot implement the AnalyticsListener because the binding doesn't work.

                //Todo: implement IAnalyticsListener
                //player.AddAnalyticsListener(eventLogger);

                player.AddAudioDebugListener(eventLogger);
                player.AddVideoDebugListener(eventLogger);

                player.AddMetadataOutput(eventLogger);
                //end Todo

                playerView.Player = player;
                playerView.SetPlaybackPreparer(this);
                debugViewHelper = new DebugTextViewHelper(player, debugTextView);
                debugViewHelper.Start();

                IMediaSource[] mediaSources = new IMediaSource[uris.Length];
                for (int i = 0; i < uris.Length; i++)
                {
                    mediaSources[i] = BuildMediaSource(uris[i], extensions[i]);
                }
                mediaSource =
                    mediaSources.Length == 1 ? mediaSources[0] : new ConcatenatingMediaSource(mediaSources);
                string adTagUristring = intent.GetStringExtra(AD_TAG_URI_EXTRA);
                if (adTagUristring != null)
                {
                    android.Net.Uri adTagUri = android.Net.Uri.Parse(adTagUristring);
                    if (!adTagUri.Equals(loadedAdTagUri))
                    {
                        ReleaseAdsLoader();
                        loadedAdTagUri = adTagUri;
                    }
                    IMediaSource adsMediaSource = CreateAdsMediaSource(mediaSource, android.Net.Uri.Parse(adTagUristring));
                    if (adsMediaSource != null)
                    {
                        mediaSource = adsMediaSource;
                    }
                    else
                    {
                        ShowToast(Resource.String.ima_not_loaded);
                    }
                }
                else
                {
                    ReleaseAdsLoader();
                }
            }
            bool haveStartPosition = startWindow != C.IndexUnset;

            if (haveStartPosition)
            {
                player.SeekTo(startWindow, startPosition);
            }
            player.Prepare(mediaSource, !haveStartPosition, false);
            UpdateButtonVisibilities();
        }