public GoogleCastManager()
 {
     sessionManager             = CastContext.SharedInstance.SessionManager;
     xamaSessionManagerListener = new XamSessionManagerListener(this);
     sessionManager.AddListener(xamaSessionManagerListener);
     castMediaController          = new UIMediaController();
     castMediaController.Delegate = new XamMediaControllerDelegate(this);
 }
        public override void DidUpdateMediaStatus(UIMediaController mediaController, MediaStatus mediaStatus)
        {
            // Once the video has finished, let the delegate know
            // and attempt to proceed to the next session, if autoAdvance
            // is enabled

            if (mediaStatus != null)
            {
                if (mediaStatus.IdleReason == MediaPlayerIdleReason.Finished)
                {
                    googleCastManager.currentVideo = null;
                    var xdelegate = googleCastManager.gcmDelegate;
                    if (xdelegate == null)
                    {
                        return;
                    }

                    xdelegate.CurrentCastedVideoDidComplete();

                    var xplaybackController = xdelegate.playbackController;
                    if (xplaybackController != null)
                    {
                        if (xplaybackController.AutoAdvance)
                        {
                            xplaybackController.AdvanceToNext();
                        }
                    }
                }

                if (mediaStatus.IdleReason == MediaPlayerIdleReason.Error)
                {
                    googleCastManager.currentVideo = null;
                    var xdelegate = googleCastManager.gcmDelegate;
                    if (xdelegate == null)
                    {
                        return;
                    }

                    xdelegate.CastedVideoFailedToPlay();
                }

                googleCastManager.castStreamPosition = mediaStatus.StreamPosition;
            }
            else
            {
                googleCastManager.castStreamPosition = 0;
            }
        }
Beispiel #3
0
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);



            if (e.OldElement != null || Element == null)
            {
                return;
            }

            try
            {
                SetupChromecastThings();
                SetupUserInterface();
                StartVideoPlayback();
                currentPage = Element;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"\t\t\tERROR: {ex.Message}");
            }

            void StartVideoPlayback()
            {
                var hasConnection = sessionManager.HasConnectedSession;

                if (hasConnection)
                {
                    mLocation = PlaybackLocation.REMOTE;
                    SwitchToRemotePlayback();
                    ClosePage();
                }

                if (mLocation == PlaybackLocation.LOCAL)
                {
                    avp.Play();
                }
            }

            void SetupUserInterface()
            {
                url          = NSUrl.FromString(mediaInfo.SourceURL);
                avp          = new AVPlayer(url);
                avpvc        = new AVPlayerViewController();
                avpvc.Player = avp;
                AddChildViewController(avpvc);
                avpvc.View.Frame            = new CGRect(0, 100, 375, 300);
                avpvc.ShowsPlaybackControls = true;
                View.AddSubview(avpvc.View);

                var castButton = new UICastButton(new CGRect(50, 20, 24, 24));

                View.AddSubview(castButton);
            }

            void SetupChromecastThings()
            {
                //setup Cast session manager
                sessionManager             = CastContext.SharedInstance.SessionManager;
                xamaSessionManagerListener = new XamSessionManagerListener(this);
                sessionManager.AddListener(xamaSessionManagerListener);

                //castMediaController
                castMediaController          = new UIMediaController();
                castMediaController.Delegate = new XamMediaControllerDelegate();
            }
        }