0

I have function for searching videos in folder. For every video file in that folder I am adding MediaElement and starts playing. When I have cca 10 videos it was all right. Then I added some videos, change view element from grid to canvas (because of performance) and now some mediaelement didn´t show (there is blank place where they should be). It is not one and same video. Mostly these happens to videos that are later procesing but not always. Does anyone know where could be problem? I think it is not problem with performance because the rest of videos are playing all right and application working fine. So is there some limitations? Or what I am doing wrong? Testing videos are .wmv and low quality (320x240).

View:

<Canvas x:Name="mainCanvas">
            <ScrollViewer HorizontalScrollBarVisibility="Auto" Width="1680" Height="750">
                <Canvas x:Name="videoCanvas">
                </Canvas>
            </ScrollViewer>
</Canvas>

and code from MainWindow.xaml.cs

 public MainWindow()
        {
            InitializeComponent();

            this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;

            getAllVideosFromFolder(System.IO.Path.GetFullPath(@"Videos\"));
        }

        private void getAllVideosFromFolder(string path)
        {
            try
            {
                var videoFiles = DirectoryHelper.GetFilesByExtensions(new DirectoryInfo(path), ".wmv", ".mp4", ".avi", ".mov");
                int i = 0, j = 0;

                foreach (var item in videoFiles)
                {
                    MediaElement melem = createMediaElementForPreview(item.FileName);

                    Canvas.SetTop(melem, i * 250);
                    Canvas.SetLeft(melem, j * 340);
                    videoCanvas.Children.Add(melem);

                    i++;
                    if (i > 2)
                    {
                        i = 0;
                        j++;
                    }

                    videos.Add(videoClass);
                }
                videoCanvas.Width = j * 340;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private MediaElement createMediaElementForPreview(string sourcePath)
        {
            MediaElement melem = new MediaElement();
            melem.LoadedBehavior = MediaState.Manual;
            melem.Source = new Uri(sourcePath, UriKind.Relative);
        melem.Width = 320;
        melem.Height = 240;
        melem.Volume = 0;
        melem.Play();
        melem.MouseDown += melem_MouseDown;
        return melem;
    }
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

3

There is no problem. MediaElement is complete shit and has many catastrophic bugs. It's fine as long you use only one media element and bad quality video.

Resort to your own DirectShow solution or see WPF MediaKit.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • I left this open a little longer but you are probably right. Now I am trying MediaKit. – Libor Zapletal Oct 18 '12 at 18:34
  • Did you try MediaUriElement create just in code? I can create playing video in xaml but with all in code I have problem. There is nothing shown and no error message. I found same problem in discussion in codeplax and not yet found sample with creating MediaUriElement just in code. – Libor Zapletal Oct 18 '12 at 18:49
  • MediaUriElement can be created just in code. There must be `BeginInit()` and `EndInit()` methods before and after setting properties. Thanks for this. MediaKit is really good working. – Libor Zapletal Oct 19 '12 at 12:44