WPF - 一次播放媒体元素的限制 [英] WPF - limitation for playing mediaelement at once

查看:55
本文介绍了WPF - 一次播放媒体元素的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在文件夹中搜索视频的功能.对于该文件夹中的每个视频文件,我都添加了 MediaElement 并开始播放.当我有 cca​​ 10 视频时,一切都很好.然后我添加了一些视频,将视图元素从网格更改为画布(由于性能),现在一些媒体元素没有显示(它们应该是空白的地方).这不是一个完全相同的视频.大多数情况下,这些发生在稍后处理的视频中,但并非总是如此.有谁知道哪里可能有问题?我认为这不是性能问题,因为其余视频播放正常,应用程序运行正常.那么是否有一些限制?或者我做错了什么?测试视频为 .wmv 和低质量 (320x240).

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).

查看:

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

和来自 MainWindow.xaml.cs 的代码

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

推荐答案

没有问题.MediaElement 是一团糟,有许多灾难性的错误.只要您只使用一种媒体元素和劣质视频就可以了.

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.

使用您自己的 DirectShow 解决方案或查看 WPF MediaKit.

Resort to your own DirectShow solution or see WPF MediaKit.

这篇关于WPF - 一次播放媒体元素的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆