XamarinMediaManager icon indicating copy to clipboard operation
XamarinMediaManager copied to clipboard

How to attach VideoView in Xamarin.Forms to CrossMediaManager.Current.MediaPlayer.VideoView

Open lekve opened this issue 4 years ago • 5 comments

First of all thanks for the hard work and this library. I have multiple VideoView s in Xamarin.Forms project in Tabbed Page , I want to attach current tab's VideoView to MediaPlayer , unfortunately its only Xamarin.Forms View not IVideoView .

lekve avatar May 20 '20 15:05 lekve

I also have the same question. How could we attach VideoView in Xamarin.Forms

gabolarraguivel avatar May 25 '20 20:05 gabolarraguivel

For Android, change VideoViewRenderer (MediaManager.Forms/Platforms/Android/VideoViewRenderer.cs)

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            //base.Element?.Dispose(); // -- remove this line
            _videoView?.Dispose();
            _videoView = null;
        }

elaurentin avatar Aug 27 '20 08:08 elaurentin

I would really like to know.

I have multiple players in 1 page and right now they sync up with each other. Attaching to the same source.

LittleBoxOfChicken avatar Feb 18 '21 14:02 LittleBoxOfChicken

Apparently you can do this.

public class CustomVideoViewRendererAndroid : VideoViewRenderer
{
    private MediaManager.Platforms.Android.Video.VideoView _videoView;

    public CustomVideoViewRendererAndroid(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<VideoView> args)
    {
        if (args.OldElement != null)
        {
            ((CustomVideoView)args.OldElement).NativeVideoView = null;
            args.OldElement.Dispose();
        }

        if (args.NewElement != null)
        {
            if (Control == null)
            {
                _videoView = new MediaManager.Platforms.Android.Video.VideoView(Context);
                CrossMediaManager.Current.MediaPlayer.VideoView = _videoView;
                SetNativeControl(_videoView);
                UpdateBackgroundColor();
                UpdateLayout();
            }
            ((CustomVideoView)args.NewElement).NativeVideoView = _videoView;
        }

        base.OnElementChanged(args);
    }

    protected override void UpdateBackgroundColor()
    {
        base.UpdateBackgroundColor();
        Control?.SetShutterBackgroundColor(Element.BackgroundColor.ToAndroid());
    }
}

As far as I know you can't attach it in any other place than OnElementChanged()

Trying to set the VideoView to anything outside of this method will not work in Xamarin.Forms.

LittleBoxOfChicken avatar Feb 19 '21 12:02 LittleBoxOfChicken

@LittleBoxOfChicken, assuming ElementChangedEventArgs<VideoView> gets VideoView from the MediaManager.Forms namespace, where does CustomVideoView exist?
Thank you.

kencherasaro avatar Jun 09 '21 01:06 kencherasaro