/// <summary> /// Initialize VideoPlayer. /// </summary> private static void Initialize(VideoStretch stretch) { #if NETFX_CORE if (_videoPopup == null) { _videoPopup = new Popup(); } _videoPopup.VerticalOffset = 0; _videoPopup.HorizontalOffset = 0; if (_videoElement == null) { _videoElement = new MediaElement(); } _videoPopup.Child = _videoElement; _videoElement.MediaEnded += _videoElement_MediaEnded; _videoElement.MediaOpened += _videoElement_MediaOpened; _videoPopup.Height = Window.Current.Bounds.Height; _videoPopup.Width = Window.Current.Bounds.Width; _videoElement.Tapped += _videoElement_Tapped; _videoElement.Stretch = (Stretch)stretch; _videoElement.AutoPlay = false; _videoElement.Height = _videoPopup.Height; _videoElement.Width = _videoPopup.Width; _videoPopup.IsOpen = true; #endif }
/// <summary> /// Plays the video given path of the file. /// There's a bug causing Video to remain on the screen after it finishes playing on WP8. /// Submitted a bug report to Unity: http://fogbugz.unity3d.com/default.asp?663800_4o1v5omb7fan6gfq /// </summary> public static void PlayVideo(string filename, Action onVideoEnded, VideoStretch stretch = VideoStretch.None) { #if NETFX_CORE Dispatcher.InvokeOnUIThread(() => { Initialize(stretch); IsPlaying = true; #if NETFX_CORE //_videoPopup.Height = Window.Current.Bounds.Height; //_videoPopup.Width = Window.Current.Bounds.Width; //_videoElement.Height = _videoPopup.Height; //_videoElement.Width = _videoPopup.Width; #endif _onVideoEnded = onVideoEnded; _videoElement.Source = new Uri(filename, UriKind.Absolute); }); #else throw new PlatformNotSupportedException(""); #endif }
internal void UpdateVideoStretch(VideoStretch stretch) { if (_player != null && RenderSurface is SurfaceView surface && !_isUpdatingStretch) { try { _isUpdatingStretch = true; var parent = (View)surface.Parent; var width = parent.Width; var height = parent.Height; var parentRatio = (double)width / height; var videoWidth = _player.VideoWidth; var videoHeight = _player.VideoHeight; var ratio = (double)_player.VideoWidth / _player.VideoHeight; _currentStretch = stretch; switch (stretch) { case VideoStretch.Fill: var fillHeight = height != 0 ? height : width / ratio; surface.Layout(0, 0, width, (int)fillHeight); break; case VideoStretch.Uniform: if (parentRatio < ratio) { var uniformHeight = height - (width / ratio); surface.Layout(0, (int)(uniformHeight / 2), width, height - (int)(uniformHeight / 2)); } else { var uniformWidth = width - (height * ratio); surface.Layout((int)(uniformWidth / 2), 0, width - (int)(uniformWidth / 2), height); } break; case VideoStretch.UniformToFill: if (parentRatio < ratio) { var uniformFillWidth = (height * ratio) - width; surface.Layout(-(int)(uniformFillWidth / 2), 0, width + (int)(uniformFillWidth / 2), height); } else { var uniformFillHeight = (width / ratio) - height; surface.Layout(0, -(int)(uniformFillHeight / 2), width, height + (int)(uniformFillHeight / 2)); } break; case VideoStretch.None: default: var noneHeight = videoHeight - height; var nonewidth = videoWidth - width; surface.Layout(-nonewidth / 2, -noneHeight / 2, width + (nonewidth / 2), height + (noneHeight / 2)); break; } } finally { _isUpdatingStretch = false; } } }