Baseflow / XamarinMediaManager

Cross platform Xamarin plugin to play and control Audio and Video
https://baseflow.com
MIT License
769 stars 306 forks source link

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

Open lekve opened 4 years ago

lekve commented 4 years ago

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 .

gabolarraguivel commented 4 years ago

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

elaurentin commented 4 years ago

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;
        }
LittleBoxOfChicken commented 3 years ago

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 commented 3 years ago

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.

kencherasaro commented 3 years ago

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