0

I'd like to understand how the options for the screen adjustments such as "Fit Screen", "Original", "Best Fit" are set, if it's possible to keep only specific options.

I tried this bellow, the video opens in fill size, but if I press the Size button it still has the other options:

var myString = "Fit screen";
byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
MediaPlayer = new MediaPlayer(media) { EnableHardwareDecoding = true, AspectRatio = myString };

Also, I'd like to know if I can open the video in another view in fullscreen mode when tapping the button, just like youtube does.

Maturano
  • 951
  • 4
  • 15
  • 42

2 Answers2

1

Given that LibVLCSharp is fully opensource, I encourage you to have a look at the code whenever you have questions.

Aspect ratio management is provided as a feature of the MediaPlayerElement component, but you can easily retrieve and use that code if you are not using that component.

private void UpdateAspectRatio(AspectRatio? aspectRatio = null)
{
    var mediaPlayer = MediaPlayer;
    var videoView = VideoView;
    if (aspectRatio == null)
    {
        aspectRatio = GetAspectRatio(mediaPlayer);
    }
    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 (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)aspectRatio;
        AspectRatioChanged?.Invoke(this, EventArgs.Empty);
    }
}

See here https://code.videolan.org/videolan/LibVLCSharp/blob/3.x/LibVLCSharp/Shared/MediaPlayerElement/AspectRatioManager.cs

Do note that both the AspectRatio and Scale properties need to be updated when changing aspect ratio, as they are intertwined.

I'd like to know if I can open the video in another view in fullscreen mode when tapping the button, just like youtube does.

I'm not sure what you mean by this. If you refer to navigation, see my blogpost about it.

mfkl
  • 1,914
  • 1
  • 11
  • 21
  • Thanks for sharing the source code, that will help me understand how the component works under the covers. Regarding the second question, I'm supposed to have something like this: the Video Player component at the top of the page and some other controls below, when the user taps to enter in full-screen mode, I'd like to open a new page with that video in full size. Maybe there's an open Event Handler for that. – Maturano Jan 15 '20 at 12:19
0

Fitscreen is simply stretching the video. But remember it will effect video quality. And also you will have to update it whenever video rotates or size changed.
You can try this working and tested code:

MediaPlayerElement1.MediaPlayer.AspectRatio = $"{MediaPlayerElement1.Width.ToString()}:{MediaPlayerElement1.Height.ToString()}";
MediaPlayerElement1.Scale = 0;
Sorry IwontTell
  • 466
  • 10
  • 29