如何使用LibVLCSharp复制第二个视频?

时间:2019-07-15 09:05:33

标签: c# video xamarin.forms libvlc libvlcsharp

我正在尝试创建一个弹出页面(使用Rg.Plugin.Popup),该页面必须显示从API获取的流视频。每次我导航到此页面时,我都希望根据我来自页面的参数(通过Messenger发送的消息获取它们)显示从API获取的不同视频。似乎在第一次运行时有效,但是,当我关闭“弹出”页面,然后再次打开它时,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中的videoView:

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

ViewModel:

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

}

1 个答案:

答案 0 :(得分:0)

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

_tkinter.TclError: character U+1f532 is above the range (U+0000-U+FFFF) allowed by Tcl