Video on Background in a content page not go in background

illomiillomi USMember ✭✭

Hi,
i have a problem to play a video in the background of a content page.
I use an absolute layout to make a custom renderer class in background and an image and two label in front of the page.
This is my primary class:

    public class MainLoginPage : ContentPage
        {
            float widht;
            float height;
            public int play =0;
            bool flag= true;
            AbsoluteLayout absoluteLayout;
            Image immagineNetForYou; 
            MyQuicksandRegularLabel bottoneLogin,bottoneRegistrati;
            public MainLoginPage (float w,float h)
            {

                widht=w;
                height = h;
                Command<Type> navigateCommand =
                    new Command<Type>(async (Type pageType) =>
                        {
                            Page page = (Page)Activator.CreateInstance(pageType);
                            await this.Navigation.PushAsync(page);
                        });

                //this.Title = "NetForYou";
                NavigationPage.SetHasNavigationBar (this, false);
                //this.BackgroundImage = "netwintec.jpg";
                absoluteLayout = new AbsoluteLayout
                {
                    //BackgroundColor = Color.Transparent,
                    //VerticalOptions = LayoutOptions.FillAndExpand,
                    //VerticalOptions = LayoutOptions.Center,
                    //HorizontalOptions = LayoutOptions.Center,
                };

                immagineNetForYou = new Image { Aspect = Aspect.Fill };
                immagineNetForYou.Source = ImageSource.FromFile ("logo_pagina_iniziale.png");

                var fsReg = new FormattedString ();
                fsReg.Spans.Add (new Span { Text="R",  FontSize = 16 });
                fsReg.Spans.Add (new Span { Text="EGISTRATI", FontSize = 11 });

                var fsLogconFB = new FormattedString ();
                fsLogconFB .Spans.Add (new Span { Text="L",  FontSize =  16 });
                fsLogconFB .Spans.Add (new Span { Text="OGIN WITH ", FontSize = 11 });
                fsLogconFB .Spans.Add (new Span { Text="F",  FontSize = 16 });
                fsLogconFB .Spans.Add (new Span { Text="ACEBOOK", FontSize = 11 });

                bottoneLogin = new MyQuicksandRegularLabel {
                    FormattedText = fsLogconFB,
                    //Text = App.Instance.Tit1,
                    FontFamily = Device.OnPlatform (
                        "Quicksand",
                        null,
                        null
                    ),
                    TextColor=Color.White,
                    //HeightRequest = 50,
                    //WidthRequest = (widht/2)-2,
                    BackgroundColor= Color.FromHex("3D3D3F"),
                    XAlign = TextAlignment.Center,
                    YAlign = TextAlignment.Center,
                    //HorizontalOptions = LayoutOptions.Center,
                };

                var tapGestureRecognizer = new TapGestureRecognizer ();
                tapGestureRecognizer.Tapped += OnTapGestureRecognizerTapped;
                bottoneLogin.GestureRecognizers.Add (tapGestureRecognizer);

                bottoneRegistrati = new MyQuicksandRegularLabel {
                    FormattedText = fsReg,
                    //Text = App.Instance.Tit1,
                    FontFamily = Device.OnPlatform (
                        "Quicksand",
                        null,
                        null
                    ),
                    TextColor=Color.White,
                    //HeightRequest = 50,
                    //WidthRequest = (widht/2)-2,
                    BackgroundColor= Color.FromHex("3D3D3F"),
                    XAlign = TextAlignment.Center,
                    YAlign = TextAlignment.Center,
                    //HorizontalOptions = LayoutOptions.Center,
                };

                var tapGestureRecognizer2 = new TapGestureRecognizer ();
                tapGestureRecognizer2.Tapped += OnTapGestureRecognizerTapped2;
                bottoneRegistrati.GestureRecognizers.Add (tapGestureRecognizer2);

                ViewBackground viewBackground = new ViewBackground ();

                AbsoluteLayout.SetLayoutFlags (viewBackground,
                    AbsoluteLayoutFlags.None);

                AbsoluteLayout.SetLayoutBounds (viewBackground,
                    new Rectangle (0, 0,widht, height));


                AbsoluteLayout.SetLayoutFlags (immagineNetForYou,
                    AbsoluteLayoutFlags.None);

                AbsoluteLayout.SetLayoutBounds (immagineNetForYou,
                    new Rectangle ((widht/2)-(height/8), (height/8),(height/4), (height/4)));

                AbsoluteLayout.SetLayoutFlags (bottoneLogin,
                    AbsoluteLayoutFlags.None);

                AbsoluteLayout.SetLayoutBounds (bottoneLogin,
                    new Rectangle (1f, height-(height/8)-25, (widht/2)-2, (height/8)));

                AbsoluteLayout.SetLayoutFlags (bottoneRegistrati,
                    AbsoluteLayoutFlags.None);

                AbsoluteLayout.SetLayoutBounds (bottoneRegistrati,
                    new Rectangle ((widht/2)+1, height-(height/8)-25, (widht/2)-2, (height/8)));

                absoluteLayout.Children.Add (viewBackground);
                absoluteLayout.Children.Add (immagineNetForYou);
                absoluteLayout.Children.Add (bottoneRegistrati);
                absoluteLayout.Children.Add (immagineLogin);

                Content = absoluteLayout;

            }

            void OnTapGestureRecognizerTapped(object sender, EventArgs args)
            {
                Navigation.PushModalAsync(new LoginPage());
            }

            void OnTapGestureRecognizerTapped2(object sender, EventArgs args)
            {
                var result = this.DisplayAlert("Cooming Soon", "Stiamo Lavorando Per Voi", "Exit");
                System.Diagnostics.Debug.WriteLine(result);
            }

            protected override void OnAppearing ()
            {
                base.OnAppearing ();
            }
        }
    }

This is my Custom Class

    public class ViewBackground : View
        {}

And This is my android renderer class

    [assembly: ExportRenderer (typeof (ViewBackground), typeof (MainLoginPageRenderer))]

    namespace NetForYouDroid.Droid
    {
        [Activity (Label = "MainLoginPageRenderer")]            
        public class MainLoginPageRenderer : ViewRenderer,MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
        {

            VideoView videoView;
            MediaPlayer player;
            Activity activity;

            protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.View> e)
            {
                base.OnElementChanged (e);

                activity = this.Context as Activity;

                activity.SetContentView (Resource.Layout.MainLayout);

                videoView = activity.FindViewById<VideoView> (Resource.Id.videoView1);


                ISurfaceHolder holder = videoView.Holder;
                holder.SetType (SurfaceType.PushBuffers);
                holder.AddCallback( this );
                player = new  MediaPlayer ();
                Android.Content.Res.AssetFileDescriptor afd = activity.Assets.OpenFd("Video/video.mp4");
                if (afd != null) {
                    player.SetDataSource (afd.FileDescriptor, afd.StartOffset, afd.Length);
                    player.Looping = true;
                    player.SetVideoScalingMode (VideoScalingMode.ScaleToFitWithCropping);
                    player.Prepare ();
                    player.Start ();
                }
            }


            public void SurfaceCreated(ISurfaceHolder holder)
            {
                Console.WriteLine("SurfaceCreated");
                player.SetDisplay (holder);
            }
            public void SurfaceDestroyed(ISurfaceHolder holder)
            {
                Console.WriteLine("SurfaceDestroyed");
            }
            public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
            {
                Console.WriteLine("SurfaceChanged");
            }
            public void OnPrepared(MediaPlayer player)
            {

            }
        }
    }

Basically video works and play but it isn't in background and it cover the label and the image.
Someone know why it stay in front of the page instead in background.
Sorry for my english i hope anyone understand my problem.

Thanks.
Matteo

Answers

  • RemiRRemiR FRMember

    I also seek a solution to this problem. Any ideas ?

    Best regards,

  • illomiillomi USMember ✭✭

    I haven't found nothing.
    For my work i make a work around,I play an animation .gif in a web view in background of the content page.

    Matteo.

  • GeorgeCookGeorgeCook PEUniversity ✭✭✭

    android media player is really bad, in fact.. android graphics stack is awful and plays badly with opengl (which is used for video renderering), so I've also seen this kind of silly issue.

    what version of android are you targetting?

  • illomiillomi USMember ✭✭

    normally my API target level is 20/21 ( 4.4 / 5.0.1)

Sign In or Register to comment.