private void PreparePlayer(bool playWhenReady)
 {
     if (_player == null)
     {
         _player = new VideoPlayer(GetRendererBuilder());
         _player.AddListener(this);
         _player.SetCaptionListener(this);
         _player.SetMetadataListener(this);
         _player.SeekTo(_playerPosition);
         _playerNeedsPrepare = true;
         _mediaController.SetMediaPlayer(_player.PlayerControl);
         _mediaController.Enabled = true;
         _eventLogger             = new EventLogger();
         _eventLogger.StartSession();
         _player.AddListener(_eventLogger);
         _player.SetInfoListener(_eventLogger);
         _player.SetInternalErrorListener(_eventLogger);
         _debugViewHelper = new DebugTextViewHelper(_player, _debugTextView);
         _debugViewHelper.Start();
     }
     if (_playerNeedsPrepare)
     {
         _player.Prepare();
         _playerNeedsPrepare = false;
         UpdateButtonVisibilities();
     }
     _player.Surface       = _surfaceView.Holder.Surface;
     _player.PlayWhenReady = playWhenReady;
 }
        // Internal methods

        private void initializePlayer()
        {
            Intent intent        = Intent;
            bool   needNewPlayer = player == null;

            if (needNewPlayer)
            {
                var adaptiveTrackSelectionFactory =
                    new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
                trackSelector           = new DefaultTrackSelector(adaptiveTrackSelectionFactory);
                trackSelectionHelper    = new TrackSelectionHelper(trackSelector, adaptiveTrackSelectionFactory);
                lastSeenTrackGroupArray = null;
                eventLogger             = new EventLogger(trackSelector);

                var drmSchemeUuid = intent.HasExtra(DRM_SCHEME_UUID_EXTRA)
                                        ? UUID.FromString(intent.GetStringExtra(DRM_SCHEME_UUID_EXTRA)) : null;
                IDrmSessionManager drmSessionManager = null;
                if (drmSchemeUuid != null)
                {
                    var drmLicenseUrl             = intent.GetStringExtra(DRM_LICENSE_URL);
                    var keyRequestPropertiesArray = intent.GetStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES);
                    int errorStringId             = Resource.String.error_drm_unknown;
                    if (Util.Util.SdkInt < 18)
                    {
                        errorStringId = Resource.String.error_drm_not_supported;
                    }
                    else
                    {
                        try
                        {
                            drmSessionManager = BuildDrmSessionManagerV18(drmSchemeUuid, drmLicenseUrl,
                                                                          keyRequestPropertiesArray);
                        }
                        catch (UnsupportedDrmException e)
                        {
                            errorStringId = e.Reason == UnsupportedDrmException.ReasonUnsupportedScheme
                                                                ? Resource.String.error_drm_unsupported_scheme : Resource.String.error_drm_unknown;
                        }
                    }
                    if (drmSessionManager == null)
                    {
                        ShowToast(errorStringId);
                        return;
                    }
                }

                var preferExtensionDecoders = intent.GetBooleanExtra(PREFER_EXTENSION_DECODERS, false);
                var extensionRendererMode   =
                    ((DemoApplication)Application).UseExtensionRenderers()
                                                ? (preferExtensionDecoders ? DefaultRenderersFactory.ExtensionRendererModePrefer
                                                : DefaultRenderersFactory.ExtensionRendererModeOn)
                                                : DefaultRenderersFactory.ExtensionRendererModeOff;
                var renderersFactory = new DefaultRenderersFactory(this,
                                                                   drmSessionManager, extensionRendererMode);

                player = ExoPlayerFactory.NewSimpleInstance(renderersFactory, trackSelector);
                player.AddListener(this);
                player.AddListener(eventLogger);
                player.SetAudioDebugListener(eventLogger);
                player.SetVideoDebugListener(eventLogger);
                player.SetMetadataOutput(eventLogger);

                simpleExoPlayerView.Player = player;
                player.PlayWhenReady       = shouldAutoPlay;
                debugViewHelper            = new DebugTextViewHelper(player, debugTextView);
                debugViewHelper.Start();
            }
            var action = intent.Action;

            Uri[]    uris;
            string[] extensions;
            if (ACTION_VIEW.Equals(action))
            {
                uris       = new Uri[] { intent.Data };
                extensions = new string[] { intent.GetStringExtra(EXTENSION_EXTRA) };
            }
            else if (ACTION_VIEW_LIST.Equals(action))
            {
                var uriStrings = intent.GetStringArrayExtra(URI_LIST_EXTRA);
                uris = new Uri[uriStrings.Length];
                for (int i = 0; i < uriStrings.Length; i++)
                {
                    uris[i] = 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));
                return;
            }
            if (Util.Util.MaybeRequestReadExternalStoragePermission(this, uris))
            {
                // The player will be reinitialized if the permission is granted.
                return;
            }
            var mediaSources = new IMediaSource[uris.Length];

            for (var i = 0; i < uris.Length; i++)
            {
                mediaSources[i] = BuildMediaSource(uris[i], extensions[i]);
            }
            var mediaSource = mediaSources.Length == 1 ? mediaSources[0]
                                : new ConcatenatingMediaSource(mediaSources);
            var adTagUriString = intent.GetStringExtra(AD_TAG_URI_EXTRA);

            if (adTagUriString != null)
            {
                Uri adTagUri = Uri.Parse(adTagUriString);
                if (!adTagUri.Equals(loadedAdTagUri))
                {
                    ReleaseAdsLoader();
                    loadedAdTagUri = adTagUri;
                }
                try
                {
                    mediaSource = CreateAdsMediaSource(mediaSource, Uri.Parse(adTagUriString));
                }
                catch (System.Exception)
                {
                    ShowToast(Resource.String.ima_not_loaded);
                }
            }
            else
            {
                ReleaseAdsLoader();
            }
            bool haveResumePosition = resumeWindow != C.IndexUnset;

            if (haveResumePosition)
            {
                player.SeekTo(resumeWindow, resumePosition);
            }
            player.Prepare(mediaSource, !haveResumePosition, false);
            inErrorState = false;
            UpdateButtonVisibilities();
        }
Beispiel #3
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();
        }
Beispiel #4
0
        /// <summary>
        /// Initializes the player.
        /// </summary>
        public void InitializePlayer()
        {
            if (_player == null)
            {
                bool preferExtensionDecoders = _stream.PreferExtensionDecoders;

                UUID drmSchemeUuid = string.IsNullOrEmpty(DrmSchemeUuidExtra)
                                                                                   ? UUID.FromString(_stream.DrmSchemeUuidExtra) : null;

                IDrmSessionManager drmSessionManager = null;

                if (drmSchemeUuid != null)
                {
                    string   drmLicenseUrl             = _stream.DrmLicenseUrl;
                    string[] keyRequestPropertiesArray = _stream.DrmKeyRequestProperties;
                    Dictionary <string, string> keyRequestProperties;

                    if (keyRequestPropertiesArray == null || keyRequestPropertiesArray.Length < 2)
                    {
                        keyRequestProperties = null;
                    }
                    else
                    {
                        keyRequestProperties = new Dictionary <string, string>();
                        for (int i = 0; i < keyRequestPropertiesArray.Length - 1; i += 2)
                        {
                            keyRequestProperties.Add(keyRequestPropertiesArray[i],
                                                     keyRequestPropertiesArray[i + 1]);
                        }
                    }
                    try
                    {
                        drmSessionManager = BuildDrmSessionManager(drmSchemeUuid, drmLicenseUrl,
                                                                   keyRequestProperties);
                    }
                    catch (UnsupportedDrmException e)
                    {
                        int errorstringId = Util.SdkInt < 18 ? Resource.String.error_drm_not_supported
                                                                                                : (e.Reason == UnsupportedDrmException.ReasonUnsupportedScheme
                                                        ? Resource.String.error_drm_unsupported_scheme : Resource.String.error_drm_unknown);
                        ShowToast(errorstringId);

                        return;
                    }
                }

                _eventLogger = new EventLogger();
                ITrackSelectionFactory videoTrackSelectionFactory =
                    new AdaptiveVideoTrackSelection.Factory(_bandwidthMeter);
                _trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

                _trackSelectionHelper = new TrackSelectionHelper(_trackSelector, videoTrackSelectionFactory);
                _player = ExoPlayerFactory.NewSimpleInstance(_context, _trackSelector, new DefaultLoadControl());
                _player.AddListener(this);
                _player.AddListener(_eventLogger);
                _player.SetAudioDebugListener(_eventLogger);
                _player.SetVideoDebugListener(_eventLogger);
                _player.SetMetadataOutput(_eventLogger);

                _simpleExoPlayerView.Player = _player;

                if (_shouldRestorePosition)
                {
                    if (_playerPosition == C.TimeUnset)
                    {
                        _player.SeekToDefaultPosition(_playerWindow);
                    }
                    else
                    {
                        _player.SeekTo(_playerWindow, _playerPosition);
                    }
                }

                _player.PlayWhenReady = _shouldAutoPlay;
                _debugViewHelper      = new DebugTextViewHelper(_player, _debugTextView);
                _debugViewHelper.Start();
                _playerNeedsSource = true;
            }

            if (_playerNeedsSource)
            {
                //var intent = (_context as Activity).Intent;
                //string action = intent.Action;

                global::Android.Net.Uri[] uris;
                string[] extensions = {};

                uris       = new global::Android.Net.Uri[] { Android.Net.Uri.Parse(_stream.Url) };
                extensions = new string[] { _stream.ExtensionExtra };

                /*if (ActionView.Equals(action))
                 * {
                 *      uris = new global::Android.Net.Uri[] { intent.Data };
                 *      extensions = new string[] { _stream.ExtensionExtra };
                 * }
                 * else if (ActionViewList.Equals(action))
                 * {
                 *      string[] uristrings = _stream.UriListExtra;
                 *      uris = new global::Android.Net.Uri[uristrings.Length];
                 *      for (int i = 0; i < uristrings.Length; i++)
                 *      {
                 *              uris[i] = global::Android.Net.Uri.Parse(uristrings[i]);
                 *      }
                 *      extensions = _stream.ExtensionListExtra;
                 *      if (extensions == null)
                 *      {
                 *              extensions = new string[uristrings.Length];
                 *      }
                 * }
                 * else
                 * {
                 *      ShowToast(_context.GetString(Resource.String.unexpected_intent_action, action));
                 *      return;
                 * }*/

                if (Util.MaybeRequestReadExternalStoragePermission((Activity)_context, uris))
                {
                    // The player will be reinitialized if the permission is granted.
                    return;
                }

                var mediaSources = new IMediaSource[uris.Length];

                for (int i = 0; i < uris.Length; i++)
                {
                    mediaSources[i] = BuildMediaSource(uris[i], extensions[i]);
                }
                IMediaSource mediaSource = mediaSources.Length == 1 ? mediaSources[0]
                                        : new ConcatenatingMediaSource(mediaSources);
                _player.Prepare(mediaSource, !_shouldRestorePosition, true);
                _playerNeedsSource = false;
                UpdateButtonVisibilities();
            }
        }
 private void PreparePlayer(bool playWhenReady)
 {
     if (_player == null)
     {
         _player = new VideoPlayer(GetRendererBuilder());
         _player.AddListener(this);
         _player.SetCaptionListener(this);
         _player.SetMetadataListener(this);
         _player.SeekTo(_playerPosition);
         _playerNeedsPrepare = true;
         _mediaController.SetMediaPlayer(_player.PlayerControl);
         _mediaController.Enabled = true;
         _eventLogger = new EventLogger();
         _eventLogger.StartSession();
         _player.AddListener(_eventLogger);
         _player.SetInfoListener(_eventLogger);
         _player.SetInternalErrorListener(_eventLogger);
         _debugViewHelper = new DebugTextViewHelper(_player, _debugTextView);
         _debugViewHelper.Start();
     }
     if (_playerNeedsPrepare)
     {
         _player.Prepare();
         _playerNeedsPrepare = false;
         UpdateButtonVisibilities();
     }
     _player.Surface = _surfaceView.Holder.Surface;
     _player.PlayWhenReady = playWhenReady;
 }