Sascha-L / WPF-MediaKit

Microsoft Public License
363 stars 115 forks source link

VideoCaptureElement created runtime fires MediaFailed #83

Open stevefatsg opened 6 years ago

stevefatsg commented 6 years ago

When VideoCaptureElement is added in XAML I can correctly show my webcam video. If, instead, I add it runtime (see following code), after assigning the VideoCaptureDevice property, MediaFailed is fired ("Capture device not found or could not be created")

public partial class MainWindow : Window
{
    WPFMediaKit.DirectShow.Controls.VideoCaptureElement VCE;

    public MainWindow()
    {
        InitializeComponent();

        VCE = new WPFMediaKit.DirectShow.Controls.VideoCaptureElement();
        Content = VCE;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        VCE.MediaOpened += VCE_MediaOpened;
        VCE.MediaFailed += VCE_MediaFailed;
        VCE.VideoCaptureDevice = WPFMediaKit.DirectShow.Controls.MultimediaUtil.VideoInputDevices[0];  // This is my laptop webcam
    }

    void VCE_MediaOpened(Object sender, RoutedEventArgs e) { ... }

    void VCE_MediaFailed(object sender, WPFMediaKit.DirectShow.MediaPlayers.MediaFailedEventArgs e) { ... }
}
stf1981 commented 4 years ago

I have the same error. The reason for the error is the property VideoCaptureElement.HasInitialized from the base class MediaElementBase. The property is always false when the VideoCaptureElement is added at Runtime. When these set manuelly to true the Webcam Video are showing as expected.