0

如果可以仅保留特定选项,我想了解如何设置“适合屏幕”、“原始”、“最佳适合”等屏幕调整选项。

我试过这个,视频以填充大小打开,但如果我按下大小按钮,它仍然有其他选项:

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

另外,我想知道我是否可以在点击按钮时以全屏模式在另一个视图中打开视频,就像 youtube 一样。

4

2 回答 2

1

鉴于 LibVLCSharp 是完全开源的,我鼓励您在有问题时查看代码。

纵横比管理作为组件的一项功能提供MediaPlayerElement,但如果您不使用该组件,则可以轻松检索和使用该代码。

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);
    }
}

见这里https://code.videolan.org/videolan/LibVLCSharp/blob/3.x/LibVLCSharp/Shared/MediaPlayerElement/AspectRatioManager.cs

请注意,更改纵横比时需要更新AspectRatio和属性,因为它们是交织在一起的。Scale

我想知道点击按钮时是否可以在另一个视图中以全屏模式打开视频,就像 youtube 一样。

我不确定你的意思。如果您提到导航,请参阅我的博客文章。

于 2020-01-15T03:49:40.053 回答
0

Fitscreen只是简单地拉伸视频。但请记住,它会影响视频质量。而且,每当视频旋转或大小发生变化时,您都必须对其进行更新。
你可以试试这个工作和测试的代码:

MediaPlayerElement1.MediaPlayer.AspectRatio = $"{MediaPlayerElement1.Width.ToString()}:{MediaPlayerElement1.Height.ToString()}";
MediaPlayerElement1.Scale = 0;
于 2020-10-13T08:11:02.260 回答