private void CleanPlayerItem() { _PlayerItem.RemoveObserver(this, _status); _PlayerItem.RemoveObserver(this, _error); _Asset.Dispose(); _PlayerItem.Dispose(); }
void synchronizePlayerWithEditor() { if (Player == null) { return; } AVPlayerItem playerItem = Editor.PlayerItem; var status = (NSString)"status"; if (this.PlayerItem != playerItem) { if (PlayerItem != null) { PlayerItem.RemoveObserver(this, status); NSNotificationCenter.DefaultCenter.RemoveObserver(observer, AVPlayerItem.DidPlayToEndTimeNotification, PlayerItem); } PlayerItem = playerItem; if (PlayerItem != null) { this.PlayerItem.SeekingWaitsForVideoCompositionRendering = true; this.PlayerItem.AddObserver(this, status, NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial, StatusObservationContext.Handle); observer = NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, (notification) => { Console.WriteLine("Seek Zero = true"); seekToZeroBeforePlaying = true; }, playerItem); //NSNotificationCenter.DefaultCenter.AddObserver (this, new MonoTouch.ObjCRuntime.Selector ("playerItemEnded:"), AVPlayerItem.DidPlayToEndTimeNotification, playerItem); } Player.ReplaceCurrentItemWithPlayerItem(playerItem); } }
public override void ViewDidDisappear(bool animated) { base.ViewDidDisappear(animated); player.Pause(); streamingItem.RemoveObserver(this, new NSString("status")); player.Dispose(); UIApplication.SharedApplication.EndReceivingRemoteControlEvents(); this.ResignFirstResponder(); }
public void KillPlayer() { this._player.Pause(); _player.RemoveObserver(this, (NSString)"rate"); this.removeTimeObserverFromPlayer(); _playerItem.RemoveObserver(this, (NSString)"status"); NSNotificationCenter.DefaultCenter.RemoveObserver(this, AVPlayerItem.DidPlayToEndTimeNotification, _playerItem); this._playerLayer.RemoveFromSuperLayer(); this._player = null; this.playerView.RemoveFromSuperview(); }
/* * MPMoviePlayerController _player; * private List<NSObject> _observers = new List<NSObject>(); * protected override void OnElementChanged(ElementChangedEventArgs<CustomVideoSlider> e) * { * base.OnElementChanged(e); * if (e.NewElement != null) * { * if (base.Control == null) * { * _player = new MPMoviePlayerController(); * _player.ScalingMode = MPMovieScalingMode.AspectFit; * * _player.ShouldAutoplay = true; * _player.ControlStyle = MPMovieControlStyle.None; * _player.MovieControlMode=MPMovieControlMode.Hidden; * _player.View.Frame = this.Bounds; * _player.BackgroundColor = UIColor.Clear; * base.SetNativeControl(_player.View); * var center = NSNotificationCenter.DefaultCenter; * // _observers.Add(center.AddObserver((NSString)"UIDeviceOrientationDidChangeNotification", DeviceRotated)); * _observers.Add(center.AddObserver(MPMoviePlayerController.PlaybackStateDidChangeNotification, OnLoadStateChanged)); * _observers.Add(center.AddObserver(MPMoviePlayerController.PlaybackDidFinishNotification, OnPlaybackComplete)); * _observers.Add(center.AddObserver(MPMoviePlayerController.WillExitFullscreenNotification, OnExitFullscreen)); * _observers.Add(center.AddObserver(MPMoviePlayerController.WillEnterFullscreenNotification, OnEnterFullscreen)); * * * * bootomToolbarView = new UIView() * { * TranslatesAutoresizingMaskIntoConstraints = false, * }; * * bootomToolbarView.AddObserver(this, (NSString)"bounds", NSKeyValueObservingOptions.New, IntPtr.Zero); * * gradientLayer = new CAGradientLayer(); * //gradientLayer.Frame = Bounds; * gradientLayer.Colors = new CGColor[] { UIColor.Clear.CGColor, UIColor.Orange.CGColor }; * gradientLayer.Locations= new NSNumber[2] {new NSNumber(0.7),new NSNumber(1.2)}; * bootomToolbarView.Layer.AddSublayer(gradientLayer); * this.InsertSubviewAbove(bootomToolbarView, _player.View); * * labelTimeLeft = new UILabel() * { * TranslatesAutoresizingMaskIntoConstraints = false, * Text = string.Format("{0:mm\\:ss}", TimeSpan.FromMilliseconds(slider.FileDuration ?? 0)), * TextColor = UIColor.White * }; * bootomToolbarView.AddSubview(labelTimeLeft); * * labelTimePlayed = new UILabel() * { * TranslatesAutoresizingMaskIntoConstraints = false, * Text = "00:00", * TextColor = UIColor.White * }; * * bootomToolbarView.AddSubview(labelTimePlayed); * * seekBar = new UISlider() * { * TranslatesAutoresizingMaskIntoConstraints = false, * MaximumTrackTintColor = Color.LightGray.ToUIColor(), * MinimumTrackTintColor = Color.White.ToUIColor(), * ThumbTintColor = Color.FromHex("#827DCE").ToUIColor() * * }; * * seekBar.TouchDown += Control_TouchDown; * seekBar.TouchUpInside += Control_TouchUpInside; * * bootomToolbarView.AddSubview(seekBar); * * bootomToolbarView.RightAnchor.ConstraintEqualTo(RightAnchor).Active = true; * bootomToolbarView.LeftAnchor.ConstraintEqualTo(LeftAnchor).Active = true; * bootomToolbarView.BottomAnchor.ConstraintEqualTo(BottomAnchor).Active = true; * bootomToolbarView.HeightAnchor.ConstraintEqualTo(40).Active = true; * * * labelTimeLeft.RightAnchor.ConstraintEqualTo(bootomToolbarView.RightAnchor, -10).Active = true; * labelTimeLeft.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; * labelTimeLeft.WidthAnchor.ConstraintEqualTo(50).Active = true; * labelTimeLeft.HeightAnchor.ConstraintEqualTo(20).Active = true; * * labelTimePlayed.LeftAnchor.ConstraintEqualTo(bootomToolbarView.LeftAnchor, 10).Active = true; * labelTimePlayed.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; * labelTimePlayed.WidthAnchor.ConstraintEqualTo(50).Active = true; * labelTimePlayed.HeightAnchor.ConstraintEqualTo(20).Active = true; * * seekBar.LeftAnchor.ConstraintEqualTo(labelTimePlayed.RightAnchor, 10).Active = true; * seekBar.RightAnchor.ConstraintEqualTo(labelTimeLeft.LeftAnchor, -10).Active = true; * seekBar.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; * seekBar.HeightAnchor.ConstraintEqualTo(20).Active = true; * * ToggleFullscreen(); * } * } * updateVideoPath(); * updateFullscreen(); * } * private void DeviceRotated(NSNotification notification) * { * ToggleFullscreen(); * } * * private void OnLoadStateChanged(NSNotification notification) * { * * } * * private void OnPlaybackComplete(NSNotification notification) { } * * private void OnExitFullscreen(NSNotification notification) { } * * private void OnEnterFullscreen(NSNotification notification) { } * * private void ToggleFullscreen() * { * _player.ScalingMode = MPMovieScalingMode.None; * switch (UIDevice.CurrentDevice.Orientation) * { * case UIDeviceOrientation.Portrait: * _player?.SetFullscreen(false, true); * break; * case UIDeviceOrientation.PortraitUpsideDown: * _player?.SetFullscreen(false, true); * break; * case UIDeviceOrientation.LandscapeLeft: * _player?.SetFullscreen(true, true); * break; * case UIDeviceOrientation.LandscapeRight: * _player?.SetFullscreen(true, true); * break; * } * _player.View.SetNeedsLayout(); * _player.View.SetNeedsDisplay(); * } * * protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) * { * base.OnElementPropertyChanged(sender, e); * if (e.PropertyName == CustomVideoSlider.FileUrlProperty.PropertyName) updateVideoPath(); * // if (e.PropertyName == CustomVideoSlider.FullscreenProperty.PropertyName) updateFullscreen(); * } * * public override void MovedToSuperview() * { * base.MovedToSuperview(); * } * * private void updateVideoPath() * { * if (_player == null) return; * // _player.ControlStyle = MPMovieControlStyle.Embedded; * _player.ShouldAutoplay = true; * _player.ContentUrl = !string.IsNullOrWhiteSpace(this.Element.FileUrl) ? NSUrl.FromString(this.Element.FileUrl) : null; * _player.PrepareToPlay(); * } * * private void updateFullscreen() * { * if (_player == null) return; * _player.SetFullscreen(true, true); * _player.View.SetNeedsLayout(); * _player.View.SetNeedsDisplay(); * } * * protected override void Dispose(bool disposing) * { * if (this._player != null) * { * this._player.Dispose(); * this._player = null; * } * base.Dispose(disposing); * } * */ protected override void OnElementChanged(ElementChangedEventArgs <CustomVideoPlayer> e) { base.OnElementChanged(e); if (Control == null) { //seekBar = new UISlider(); //SetNativeControl(seekBar); mainView = new UIView() { TranslatesAutoresizingMaskIntoConstraints = false, }; SetNativeControl(mainView); } if (e.OldElement != null) { videoPlayerAV.RemoveObserver(this, (NSString)"videoBounds"); bootomToolbarView.RemoveObserver(this, (NSString)"bounds"); UIApplication.SharedApplication.SetStatusBarHidden(false, true); StopPlayingAction(false); playerItem.RemoveObserver(this, (NSString)"loadedTimeRanges"); playerItem.Dispose(); player.Dispose(); Console.WriteLine("Video Finished 1"); //playerItem = null; //player = null; } if (e.NewElement != null) { slider = e.NewElement; _fitSize = Control.Bounds.Size; // except if your not running iOS 7... then it fails... if (_fitSize.Width <= 0 || _fitSize.Height <= 0) { _fitSize = new SizeF(22, 22); // Per the glorious documentation known as the SDK docs } slider.CommandImageFileName = "audioPlay.png"; slider.CommandText = "Play"; slider.StopPlayingAction = StopPlayingAction; //seekBar = new UISlider(); //seekBar.MaximumTrackTintColor = Color.LightGray.ToUIColor(); //seekBar.MinimumTrackTintColor = Color.White.ToUIColor(); //seekBar.ThumbTintColor = Color.FromHex("#827DCE").ToUIColor(); //seekBar.Continuous = false; // Control.ScaleY = 5; if (slider.FileUrl != null) { // playerItem = AVPlayerItem.FromUrl(NSUrl.FromString("https://s3.amazonaws.com/kargopolov/kukushka.mp3"));// (asset); playerItem = AVPlayerItem.FromUrl(NSUrl.FromString(slider.FileUrl));// (asset); //playerItem = AVPlayerItem.FromUrl(NSUrl.FromString("https://mindcorners.blob.core.windows.net/post-attachment-videos/6412cb27-414a-435b-a480-66af5e935cb8.mov?sv=2017-04-17&sr=b&sig=gd3mn1Gai7QZHoSpx%2Fi8x%2FHfnOutACOV7WvsVblywk0%3D&se=2017-10-11T15%3A05%3A15Z&sp=r")); playerItem.AddObserver(this, (NSString)"loadedTimeRanges", NSKeyValueObservingOptions.New, IntPtr.Zero); player = new AVPlayer(playerItem); //player.AddObserver(this, "loadedTimeRanges", NSKeyValueObservingOptions.New, null); //playerLayer = AVPlayerLayer.FromPlayer(player); videoPlayerAV = new AVPlayerViewController(); videoPlayerAV.View.BackgroundColor = UIColor.Clear; videoPlayerAV.ShowsPlaybackControls = false; videoPlayerAV.AddObserver(this, (NSString)"videoBounds", NSKeyValueObservingOptions.New, IntPtr.Zero); videoPlayerAV.View.Frame = Control.Frame; videoPlayerAV.Player = player; UITapGestureRecognizer tapGesture = new UITapGestureRecognizer(OnVideoTap); videoView = new UIView() { BackgroundColor = UIColor.Clear }; // videoView.Bounds = new CGRect(0,0,100,100); videoView.AddGestureRecognizer(tapGesture); //videoPlayerAV.View.AddSubview(videoView); //button.CenterYAnchor.ConstraintEqualTo(videoPlayerAV.ContentOverlayView.CenterYAnchor).Active = true; //button.WidthAnchor.ConstraintEqualTo(50).Active = true; //button.HeightAnchor.ConstraintEqualTo(50).Active = true; //videoPlayerAV.VideoGravity = AVLayerVideoGravity.ResizeAspect; mainView.AddSubview(videoPlayerAV.View); mainView.AddSubview(videoView); videoView.TopAnchor.ConstraintEqualTo(mainView.TopAnchor).Active = true; videoView.BottomAnchor.ConstraintEqualTo(mainView.BottomAnchor).Active = true; videoView.LeftAnchor.ConstraintEqualTo(mainView.LeftAnchor).Active = true; videoView.RightAnchor.ConstraintEqualTo(mainView.RightAnchor).Active = true; videoView.WidthAnchor.ConstraintEqualTo(mainView.WidthAnchor).Active = true; videoView.HeightAnchor.ConstraintEqualTo(mainView.HeightAnchor).Active = true; button = new UIButton() { BackgroundColor = UIColor.Clear, TitleLabel = { Text = slider.CommandText }, TranslatesAutoresizingMaskIntoConstraints = false, }; button.TouchUpInside += Button_TouchUpInside; videoView.AddSubview(button); button.CenterXAnchor.ConstraintEqualTo(videoView.CenterXAnchor).Active = true; button.CenterYAnchor.ConstraintEqualTo(videoView.CenterYAnchor).Active = true; //button.CenterXAnchor.ConstraintEqualTo(videoPlayerAV.View.CenterXAnchor).Active = true; //button.CenterYAnchor.ConstraintEqualTo(videoPlayerAV.View.CenterYAnchor).Active = true; button.WidthAnchor.ConstraintEqualTo(50).Active = true; button.HeightAnchor.ConstraintEqualTo(50).Active = true; var center = NSNotificationCenter.DefaultCenter; NSNotificationCenter.DefaultCenter.AddObserver((NSString)"UIDeviceOrientationDidChangeNotification", DeviceRotated); //mainView.Layer.AddSublayer(playerLayer); if (slider.AutoPlay) { UpdatePlaying(); } else { { slider.CommandImageFileName = "audioPlay.png"; slider.CommandText = "Play"; button.Hidden = false; //button.SetTitle("paused", UIControlState.Normal); player.Pause(); isPlaying = false; } button.SetImage(UIImage.FromFile(slider.CommandImageFileName), UIControlState.Normal); } } bootomToolbarView = new UIView() { TranslatesAutoresizingMaskIntoConstraints = false, }; bootomToolbarView.AddObserver(this, (NSString)"bounds", NSKeyValueObservingOptions.New, IntPtr.Zero); gradientLayer = new CAGradientLayer(); //gradientLayer.Frame = Bounds; gradientLayer.Colors = new CGColor[] { UIColor.Clear.CGColor, UIColor.Black.CGColor }; gradientLayer.Locations = new NSNumber[2] { new NSNumber(0.7), new NSNumber(1.2) }; bootomToolbarView.Layer.AddSublayer(gradientLayer); mainView.InsertSubviewAbove(bootomToolbarView, videoPlayerAV.View); labelTimeLeft = new UILabel() { TranslatesAutoresizingMaskIntoConstraints = false, Text = string.Format("{0:mm\\:ss}", TimeSpan.FromMilliseconds(slider.FileDuration ?? 0)), TextColor = UIColor.White }; bootomToolbarView.AddSubview(labelTimeLeft); labelTimePlayed = new UILabel() { TranslatesAutoresizingMaskIntoConstraints = false, Text = "00:00", TextColor = UIColor.White }; bootomToolbarView.AddSubview(labelTimePlayed); seekBar = new UISlider() { TranslatesAutoresizingMaskIntoConstraints = false, MaximumTrackTintColor = Color.LightGray.ToUIColor(), MinimumTrackTintColor = Color.White.ToUIColor(), ThumbTintColor = Color.FromHex("#827DCE").ToUIColor() }; seekBar.SetThumbImage(UIImage.FromFile("audioTint.png"), UIControlState.Normal); seekBar.TouchDown += Control_TouchDown; seekBar.TouchUpInside += Control_TouchUpInside; bootomToolbarView.AddSubview(seekBar); //videoPlayerAV.View.RightAnchor.ConstraintEqualTo(RightAnchor).Active = true; //videoPlayerAV.View.LeftAnchor.ConstraintEqualTo(LeftAnchor).Active = true; //videoPlayerAV.View.BottomAnchor.ConstraintEqualTo(BottomAnchor).Active = true; //videoPlayerAV.View.TopAnchor.ConstraintEqualTo(TopAnchor).Active = true; bootomToolbarView.RightAnchor.ConstraintEqualTo(RightAnchor).Active = true; bootomToolbarView.LeftAnchor.ConstraintEqualTo(LeftAnchor).Active = true; bootomToolbarView.BottomAnchor.ConstraintEqualTo(BottomAnchor).Active = true; bootomToolbarView.HeightAnchor.ConstraintEqualTo(40).Active = true; labelTimeLeft.RightAnchor.ConstraintEqualTo(bootomToolbarView.RightAnchor, -10).Active = true; labelTimeLeft.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; labelTimeLeft.WidthAnchor.ConstraintEqualTo(50).Active = true; labelTimeLeft.HeightAnchor.ConstraintEqualTo(20).Active = true; labelTimePlayed.LeftAnchor.ConstraintEqualTo(bootomToolbarView.LeftAnchor, 10).Active = true; labelTimePlayed.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; labelTimePlayed.WidthAnchor.ConstraintEqualTo(50).Active = true; labelTimePlayed.HeightAnchor.ConstraintEqualTo(20).Active = true; seekBar.LeftAnchor.ConstraintEqualTo(labelTimePlayed.RightAnchor, 10).Active = true; seekBar.RightAnchor.ConstraintEqualTo(labelTimeLeft.LeftAnchor, -10).Active = true; seekBar.BottomAnchor.ConstraintEqualTo(bootomToolbarView.BottomAnchor, -10).Active = true; seekBar.HeightAnchor.ConstraintEqualTo(20).Active = true; mainView.RightAnchor.ConstraintEqualTo(RightAnchor).Active = true; mainView.LeftAnchor.ConstraintEqualTo(LeftAnchor).Active = true; // mainView.BottomAnchor.ConstraintEqualTo(BottomAnchor).Active = true; mainView.TopAnchor.ConstraintEqualTo(TopAnchor).Active = true; // bootomToolbarView = new UIView(); // //bootomToolbarView.BackgroundColor = UIColor.Orange; // CAGradientLayer gradientLayer = new CAGradientLayer(); // gradientLayer.Frame = bootomToolbarView.Frame; // gradientLayer.Colors = new CGColor[] {UIColor.Clear.CGColor, UIColor.Orange.CGColor}; // bootomToolbarView.Layer.AddSublayer(gradientLayer); // //bootomToolbarView.BackgroundColor = new CGGradient(CGColorSpace.CreateAcesCGLinear(), new CGColor[] {UIColor.Clear.CGColor, UIColor.Black.CGColor }); // labelTimePlayed = new UILabel(); //// labelTimePlayed.Frame = new CGRect(0,10,50,20); // labelTimePlayed.Text = "00:00"; // labelTimePlayed.TextColor = UIColor.White; // //seekBar.Frame = new CGRect(50, 10, 100, 20); // labelTimeLeft = new UILabel(); // //labelTimeLeft.Frame = new CGRect(150, 10, 50, 20); // labelTimeLeft.Text = "00:00"; // labelTimeLeft.Text = string.Format("{0:mm\\:ss}", TimeSpan.FromMilliseconds(slider.FileDuration ?? 0)); // labelTimeLeft.TextColor = UIColor.White; // //bootomToolbarView.Frame = new CGRect(0, 10, 200, 20); // bootomToolbarView.AddSubview(labelTimePlayed); // bootomToolbarView.InsertSubviewAbove(labelTimeLeft, labelTimePlayed); // bootomToolbarView.InsertSubviewAbove(seekBar, labelTimeLeft); // mainView.InsertSubviewAbove(bootomToolbarView, videoPlayerAV.View); //playerLayer = AVPlayerLayer.FromPlayer(player); UIApplication.SharedApplication.SetStatusBarHidden(true, true); } //seekBar.TouchDown += Control_TouchDown; //seekBar.TouchUpInside += Control_TouchUpInside; }