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