Esempio n. 1
0
 /// <summary>
 /// Releases the player.
 /// </summary>
 private void ReleasePlayer()
 {
     if (_player != null)
     {
         _debugViewHelper.Stop();
         _debugViewHelper       = null;
         _shouldAutoPlay        = _player.PlayWhenReady;
         _shouldRestorePosition = false;
         Timeline timeline = _player.CurrentTimeline;
         if (timeline != null)
         {
             _playerWindow = _player.CurrentWindowIndex;
             Timeline.Window window = timeline.GetWindow(_playerWindow, new Timeline.Window());
             if (!window.IsDynamic)
             {
                 _shouldRestorePosition = true;
                 _playerPosition        = window.IsSeekable ? _player.CurrentPosition : C.TimeUnset;
             }
         }
         _player.Release();
         _player               = null;
         _trackSelector        = null;
         _trackSelectionHelper = null;
         _eventLogger          = null;
     }
 }
Esempio n. 2
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();
            }
        }