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;
 }
Exemple #2
0
        /// <summary>
        /// Plays the video contained in the URL at the given starting position.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="position">The position.</param>
        private void PlayVideo(string url, long?position)
        {
            LoadingSpinnerView.Visibility = ViewStates.Visible;
            LoadingSpinnerView.Start();

            //
            // Prepare the standard HTTP information for the source.
            //
            var          mediaUri  = global::Android.Net.Uri.Parse(url);
            var          userAgent = Util.GetUserAgent(Activity, "Crex");
            var          defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
            var          defaultDataSourceFactory     = new DefaultDataSourceFactory(Activity, null, defaultHttpDataSourceFactory);
            IMediaSource source;

            //
            // Determine if this is an HLS or MP4 stream.
            //
            if (mediaUri.Path.EndsWith(".m3u8", StringComparison.InvariantCultureIgnoreCase) || mediaUri.Path.EndsWith("/hsl", StringComparison.InvariantCultureIgnoreCase))
            {
                using (var factory = new HlsMediaSource.Factory(defaultDataSourceFactory))
                {
                    source = factory.CreateMediaSource(mediaUri);
                }
            }
            else
            {
                using (var factory = new ExtractorMediaSource.Factory(defaultDataSourceFactory))
                {
                    source = factory.CreateMediaSource(mediaUri);
                }
            }

            //
            // Create the player and get it ready for playback.
            //
            AutoPlay    = true;
            VideoPlayer = ExoPlayerFactory.NewSimpleInstance(Activity, new DefaultTrackSelector());
            VideoPlayer.AddListener(this);
            PlayerView.Player = VideoPlayer;

            if (position.HasValue)
            {
                VideoPlayer.SeekTo(position.Value);
            }

            VideoPlayer.Prepare(source, !position.HasValue, false);
        }