0

我正在尝试创建一个Rg.Plugin.Popup必须显示从 API 获得的流视频的弹出页面(使用)。每次我导航到这个页面时,我想显示一个不同的视频,从 API 获得,基于我来自的页面提供的参数(我通过 Messenger 发送的消息获取它们)。它似乎在第一次运行时工作但是,当我关闭弹出页面并再次打开它时,videoView 是全黑的,并且在输出中写入:Failed to get window format.

这是我的代码(基于此处提供的示例:https ://code.videolan.org/videolan/LibVLCSharp/tree/master/Samples/Forms ):

代码隐藏页面:

 public partial class WebcamVideoPopUpPage : PopupPage
{
    public WebcamVideoPopUpPage()
    {
        var vm = App.Locator.WebCamVideoVM;
        this.BindingContext = vm;
        InitializeComponent();
    }

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

    }

}

xaml 中的视频视图:

<shared:VideoView x:Name="VideoView" 
                  MediaPlayer ="{Binding MediaPlayer}"
                  HorizontalOptions ="FillAndExpand" 
                  VerticalOptions ="FillAndExpand"
                  MediaPlayerChanged ="VideoView_MediaPlayerChanged"/>

视图模型:

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();
        }
    }
    public WebcamVideoViewModel(INavigationService navigationService, IApiManagerFactory apiFactory) : base(navigationService, apiFactory)
    {
        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)
        {  
            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);
        }
    }

}
4

1 回答 1

0

我解决了覆盖 OnAppering 方法的问题:

protected override void OnDisappearing()
{
  base.OnDisappearing();
  VideoView.MediaPlayer.Stop();
  VideoView.MediaPlayer = null;
}
于 2019-07-16T10:48:35.337 回答