0

当我播放带有视频的视频时,它会在视频的顶部和底部显示黑色条纹,就像 URL https://imgur.com/a/JiUv8rt中的图像一样。我想删除乐队并以绝对布局仅显示视频。我怎样才能达到我的目标?

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
             xmlns:shared="clr-namespace:LibVLCSharp.Forms.Shared;assembly=LibVLCSharp.Forms"
             x:Class="App.Pages.WebcamVideoPopUpPage"
             BackgroundColor="Transparent">
    <pages:PopupPage.Animation>
        <animations:ScaleAnimation 
            PositionIn="Center"
            PositionOut="Center"
            ScaleIn="1.2"
            ScaleOut="0.8"
            DurationIn="400"
            DurationOut="300"
            EasingIn="SinOut"
            EasingOut="SinIn"
            HasBackgroundAnimation="True"/>
    </pages:PopupPage.Animation>

    <AbsoluteLayout x:Name="AbsoluteLayoutWebcam"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">


        <shared:VideoView x:Name="VideoViewWebcam"
                          AbsoluteLayout.LayoutFlags="All"
                          AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
                          MediaPlayer ="{Binding MediaPlayer}"
                          MediaPlayerChanged ="VideoView_MediaPlayerChanged"/>

        <Label x:Name="DescriptionWebcam"
               AbsoluteLayout.LayoutFlags="All" 
               AbsoluteLayout.LayoutBounds="0, 0, 1, .2"
               HorizontalOptions="Fill" 
               VerticalOptions="Fill" 
               Text="{Binding InfoWebcam}"
               FontSize="Large"
               TextColor="White"/>

    </AbsoluteLayout>
</pages:PopupPage>

更新 我按照@mtz 的建议在最新的预发布版本中更新,并通过以下方式修改了我的代码:

public partial class WebcamVideoPopUpPage : PopupPage
{
    public WebcamVideoPopUpPage()
    {
        var vm = App.Locator.WebCamVideoVM;
        this.BindingContext = vm;
        InitializeComponent();
        MediaPlayerWebcam.VideoView.MediaPlayerChanged += VideoView_MediaPlayerChanged;
        MediaPlayerWebcam.MediaPlayer.AspectRatio = "FitScreen";
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Messenger.Default.Send(new OnApperingVideoMessage());
    }

    private void VideoView_MediaPlayerChanged(object sender, LibVLCSharp.Shared.MediaPlayerChangedEventArgs e)
    {
        Messenger.Default.Send(new OnVideoViewInitializedMessage());
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MediaPlayerWebcam.MediaPlayer.Stop();
        MediaPlayerWebcam.MediaPlayer = null;

    }

}

我的xml:

<AbsoluteLayout x:Name="AbsoluteLayoutWebcam"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand">


    <shared:MediaPlayerElement x:Name="MediaPlayerWebcam"
                      AbsoluteLayout.LayoutFlags="All"
                      AbsoluteLayout.LayoutBounds="0, 0, 1, .4"
                      MediaPlayer ="{Binding MediaPlayer}"/>

    <Label x:Name="DescriptionWebcam"
           AbsoluteLayout.LayoutFlags="All" 
           AbsoluteLayout.LayoutBounds="0, 0, 1, .2"
           HorizontalOptions="Fill" 
           VerticalOptions="Fill" 
           Text="{Binding InfoWebcam}"
           FontSize="Large"
           TextColor="White"/>

</AbsoluteLayout>

我的视图模型:

public class WebcamVideoViewModel : BaseViewModel
{
    private LibVLC LibVLC { get; set; }

    private bool IsLoaded { get; set; }
    private bool IsVideoViewInitialized { get; set; }

    private Media Media { get; set; }

    private MediaPlayer _mediaPlayer;
    public MediaPlayer MediaPlayer
    {
        get { return _mediaPlayer; }
        set
        {
            _mediaPlayer = value;
            OnPropertyChanged();
        }
    }

    private string _InfoWebcam { get; set; }

    public string InfoWebcam
    {
        get { return _InfoWebcam; }

        set
        {
            _InfoWebcam = value;
            OnPropertyChanged();
        }
    }
    public WebcamVideoViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeFactory) : base(navigationService, apiAutostradeFactory)
    {
        Messenger.Default.Register<InfoWebcamVideoMessage>(this, OnReceivedInfoWebcam);
        Messenger.Default.Register<OnApperingVideoMessage>(this, OnAppearing);
        Messenger.Default.Register<OnVideoViewInitializedMessage>(this, OnVideoViewInitialized);
        Task.Run(Initialize);
    }

    private void Initialize()
    {
        Core.Initialize();

        LibVLC = new LibVLC();
        MediaPlayer = new MediaPlayer(LibVLC);

    }

    private async void OnReceivedInfoWebcam(InfoWebcamVideoMessage msg)
    {
        var response = await ApiManager.GetVideoWebcam(msg.Mpr, msg.Uuid);
        if (response.IsSuccessStatusCode)
        {
            InfoWebcam = msg.T_Description_webcam;
            var stream = await response.Content.ReadAsStreamAsync();
            Media = new Media(LibVLC, stream);

            Play();
        }
    }

    public void OnAppearing(OnApperingVideoMessage msg)
    {
        IsLoaded = true;

    }

    public void OnVideoViewInitialized(OnVideoViewInitializedMessage msg)
    {
        IsVideoViewInitialized = true;
    }

    private void Play()
    {
        if (IsLoaded && IsVideoViewInitialized)
        {
            MediaPlayer.Play(Media);

        }
    }

}

现在我更接近解决方案,因为 videoView 可以通过 bootm 上的按钮调整大小,但我想开始填充 AspectRatio 并且我不想要任何视频(换句话说,我想要删除搜索栏和调整视频按钮)。另一个问题是,在我关闭 mediaPlayer 并再次打开一个新视频后,我的应用程序崩溃了。有什么建议吗?

4

3 回答 3

2

您需要更改纵横比以“填满屏幕”。

在此处查看如何操作:https ://github.com/videolan/libvlcsharp/blob/91b8f06ee1bedd9b3219a4e9ade0a9c44f59fda8/LibVLCSharp.Forms/Shared/PlaybackControls.xaml.cs#L926或使用包含 MediaPlayerElement 的最新预发布 LibVLCSharp.Forms 包内置此功能(即将推出稳定版)。

于 2019-08-08T07:53:19.167 回答
0

你只是想拉伸视频。但请记住,它会影响视频质量
为简单起见,您可以尝试以下工作且经过测试的代码:

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

我的场景是全屏播放器,我用这些代码参考LibVLCSharp.Forms,希望对你有所帮助。该代码处理全屏(注释)或用视频填充屏幕。

    public void PlayerFullScreen()
    {
        //restore
        if (_isFullScreen)
        {
            RestoreDefaultWindowInfo();
            _isFullScreen = false;
            _mediaPlayer.Scale = _originalScale;   //reset 
            _mediaPlayer.AspectRatio = _originalAspectRatio;
            //Mouse.Capture(null);
            playerBar.Visibility = Visibility.Visible;
        }
        else  // fullscreen(stretch video)
        {
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            this.Left = 0;
            this.Top = 0;
            this.Width = SystemParameters.VirtualScreenWidth;
            this.Height = SystemParameters.VirtualScreenHeight;
            //this.Topmost = true;
            _isFullScreen = true;
            _originalScale = _mediaPlayer.Scale;   // save original
            _originalAspectRatio = _mediaPlayer.AspectRatio;

            playerBar.Visibility = Visibility.Collapsed;

            MediaTrack? mediaTrack;
            try
            {
                mediaTrack = _mediaPlayer.Media?.Tracks?.FirstOrDefault(x => x.TrackType == TrackType.Video);
            }
            catch (Exception)
            {
                mediaTrack = null;
            }
            if (mediaTrack == null || !mediaTrack.HasValue)
            {
                return;
            }

            //get windows scale factor(DPI)
            PresentationSource source = PresentationSource.FromVisual(this);
            double dpiX=1.0, dpiY=1.0;
            if (source != null)
            {
                dpiX = source.CompositionTarget.TransformToDevice.M11;
                dpiY = source.CompositionTarget.TransformToDevice.M22;
            }

            var displayW = this.Width * dpiX;
            var displayH = this.Height * dpiY;
            var videoSwapped = mediaTrack.Value.Data.Video.Orientation == VideoOrientation.LeftBottom ||
                                        mediaTrack.Value.Data.Video.Orientation == VideoOrientation.RightTop;

            var videoW = mediaTrack.Value.Data.Video.Width;
            var videoH = mediaTrack.Value.Data.Video.Height;

            if (videoSwapped)
            {
                var swap = videoW;
                videoW = videoH;
                videoH = swap;
            }
            if (mediaTrack.Value.Data.Video.SarNum != mediaTrack.Value.Data.Video.SarDen)
                videoW = videoW * mediaTrack.Value.Data.Video.SarNum / mediaTrack.Value.Data.Video.SarDen;

            var ar = videoW / (float)videoH;
            var dar = displayW / (float)displayH;

            float scale;
            if (dar >= ar)
                scale = (float)displayW / videoW; /* horizontal */
            else
                scale = (float)displayH / videoH; /* vertical */

            //keep ratio of width/height, not fill the srceen
            //_mediaPlayer.Scale = scale;         // 这是保持视频宽高比的,视频不会变形
            //_mediaPlayer.AspectRatio = string.Empty;//这是保持视频宽高比的,视频不会变形

            //这是视频变形适配屏幕的情况(满屏)
            //fill all the screen by video,video is streched 
            float xscale, yscale;
            xscale = (float)(displayW / videoW);
            yscale = (float)(displayH / videoH);
            _mediaPlayer.Scale = (xscale<yscale)? xscale:yscale;
            string aspectRatio = String.Format("{0}:{1}",
                    this.Width,this.Height);
            _mediaPlayer.AspectRatio = aspectRatio;

        }
    }
于 2021-01-19T01:55:46.160 回答