0

I want to fit my VideoView to screen size in xamarin forms ios, enter image description here

I use it but i can't get fit size

 MediaPlayer = new MediaPlayer(LibVLC)
 {
       Media = media,
       EnableHardwareDecoding = true,
       AspectRatio = $"{App.ScreenWidth}:{App.ScreenHeight}"
 };

so I used fullscreen property

 MediaPlayer = new MediaPlayer(LibVLC)
 {
       Media = media,
       EnableHardwareDecoding = true,
       Fullscreen = true
 };

It couldn't give my want result i made custom renderer and overridden OnSizeAllocated() Method, I guessed that changing the videoview size would change the video size as well.

   protected override void OnSizeAllocated(double width, double height)
   {
         base.OnSizeAllocated(width, height);

         if (width > height)
         {
              if (height * 16 / 9 > width)
              {
                   VideoView.WidthRequest = width;
                   VideoView.HeightRequest = width * 9 / 16;
              }
              else
              {
                   VideoView.HeightRequest = height;
                   VideoView.WidthRequest = height * 16 / 9;
              }
         }
         else
         {
              if (width * 9 / 16 > height)
              {
                  VideoView.HeightRequest = height;
                  VideoView.WidthRequest = height * 16 / 9;
              }
              else
              {
                  VideoView.WidthRequest = width;
                  VideoView.HeightRequest = width * 9 / 16;
              }
         }
   }

Likewise I didn't get the desired result In addition, I tried various methods, for example, --fullscreen option, etc.

if i has some mistake, please advice for me

https://github.com/Sunday5214/vlcExample

above link is my example

after I checked answer, i tried solution

        MediaPlayer = new MediaPlayer(LibVLC)
        {
            Media = media,
            EnableHardwareDecoding = true,
            Scale = 0,
            AspectRatio = $"{App.ScreenHeight}:{App.ScreenWidth}"
        };

still i couldn't get fullscreen,

i got to know some interest things, if i rotate my phone to landscape then rotate to portrait, i can get full screen

enter image description here

before rotating,

enter image description here

after rotating

Sunday
  • 109
  • 1
  • 9

1 Answers1

0

This is how you are supposed to change aspect ratio

private void UpdateAspectRatio(AspectRatio? aspectRatio = null)
{
    var mediaPlayer = MediaPlayer;
    var videoView = VideoView;
    if (aspectRatio == null)
    {
        aspectRatio = _aspectRatio ?? GetAspectRatio(mediaPlayer);
        if (aspectRatio == null)
        {
            return;
        }
    }
    if (videoView != null && mediaPlayer != null)
    {
        switch (aspectRatio)
        {
            case AspectRatio.Original:
                mediaPlayer.AspectRatio = null;
                mediaPlayer.Scale = 1;
                break;
            case AspectRatio.Fill:
                var videoTrack = GetVideoTrack(mediaPlayer);
                if (videoTrack == null)
                {
                    break;
                }
                mediaPlayer.Scale = 0;
                mediaPlayer.AspectRatio = IsVideoSwapped((VideoTrack)videoTrack) ? $"{videoView.Height}:{videoView.Width}" :
                    $"{videoView.Width}:{videoView.Height}";
                break;
            case AspectRatio.BestFit:
                mediaPlayer.AspectRatio = null;
                mediaPlayer.Scale = 0;
                break;
            case AspectRatio.FitScreen:
                videoTrack = GetVideoTrack(mediaPlayer);
                if (videoTrack == null)
                {
                    break;
                }
                var track = (VideoTrack)videoTrack;
                var videoSwapped = IsVideoSwapped(track);
                var videoWidth = videoSwapped ? track.Height : track.Width;
                var videoHeigth = videoSwapped ? track.Width : track.Height;
                if (videoWidth == 0 || videoHeigth == 0)
                {
                    mediaPlayer.Scale = 0;
                }
                else
                {
                    if (track.SarNum != track.SarDen)
                    {
                        videoWidth = videoWidth * track.SarNum / track.SarDen;
                    }

                    var ar = videoWidth / (double)videoHeigth;
                    var videoViewWidth = videoView.Width;
                    var videoViewHeight = videoView.Height;
                    var dar = videoViewWidth / videoViewHeight;

                    var rawPixelsPerViewPixel = DisplayInformation.ScalingFactor;
                    var displayWidth = videoViewWidth * rawPixelsPerViewPixel;
                    var displayHeight = videoViewHeight * rawPixelsPerViewPixel;
                    mediaPlayer.Scale = (float)(dar >= ar ? (displayWidth / videoWidth) : (displayHeight / videoHeigth));
                }
                mediaPlayer.AspectRatio = null;
                break;
            case AspectRatio._16_9:
                mediaPlayer.AspectRatio = "16:9";
                mediaPlayer.Scale = 0;
                break;
            case AspectRatio._4_3:
                mediaPlayer.AspectRatio = "4:3";
                mediaPlayer.Scale = 0;
                break;
        }
    }

    if (_aspectRatio != aspectRatio)
    {
        _aspectRatio = aspectRatio;
        AspectRatioChanged?.Invoke(this, EventArgs.Empty);
    }
}

You need to set both the Scale and AspectRatio properties. This is a helper only available from the MediaPlayerElement when using Xamarin.Forms, but you can copy that behavior in your app.

mfkl
  • 1,914
  • 1
  • 11
  • 21
  • I checked about this code, but i couldn't understand, if i set Scale and Aspect Ratio in Media Player Constructor, media should resize to my setting value. but it was not resize – Sunday Jun 09 '21 at 06:00