MediaElement endedEvent保持循环

时间:2015-05-01 15:07:41

标签: c# wpf video mediaelement

这是我的问题,我有一个程序可以读取带有视频名称的字符串列表,另一个带有“填充视频”的程序,只要当前正在播放的视频不存在或视频填充已达到它时播放结束。下面是代码:

        private const string VIDEO_EXTENSION = "*.mp4";
        private int randIndex = 0, seconds = 0;
        private string vidName = null;
        private static string DIRECTORY = System.Configuration.ConfigurationManager.AppSettings["Directory"];

        private DirectoryInfo directoryInfo = new DirectoryInfo(DIRECTORY);
        private DispatcherTimer videoPlaytimVerification = new System.Windows.Threading.DispatcherTimer();
        private List<VideoEntity.Video> videoPlayList = new List<VideoEntity.Video>();
        private List<VideoEntity.VideoTails> videoPlayListFill = new List<VideoEntity.VideoTails>();

    MediaElement me = new MediaElement();

    Log log = new Log();

    #endregion

    public MainWindow()
    {
        InitializeComponent();

        seconds = (int)DateTime.Now.TimeOfDay.TotalSeconds;

        VideoEntity.Video Video1 = new VideoEntity.Video(1, 10, 10, "572ce78adc.mp4", 52800);
        VideoEntity.Video Video2 = new VideoEntity.Video(1, 10, 10, "025158ad58.mp4", 52810);
        VideoEntity.Video Video3 = new VideoEntity.Video(1, 10, 10, "3b630e28b6.mp4", 52820);
        VideoEntity.Video Video4 = new VideoEntity.Video(1, 10, 10, "49fcb4efba.mp4", 52830);
        VideoEntity.Video Video5 = new VideoEntity.Video(1, 10, 10, "572ce78adc.mp4", 52840);
        VideoEntity.Video Video6 = new VideoEntity.Video(1, 10, 10, "025158ad58.mp4", 52850);
        VideoEntity.Video Video7 = new VideoEntity.Video(1, 10, 10, "3b630e28b6.mp4", 52860);
        VideoEntity.Video Video8 = new VideoEntity.Video(1, 10, 10, "49fcb4efba.mp4", 52870);

        videoPlayList.Add(Video1);
        videoPlayList.Add(Video2);
        videoPlayList.Add(Video3);
        videoPlayList.Add(Video4);
        videoPlayList.Add(Video5);
        videoPlayList.Add(Video6);
        videoPlayList.Add(Video7);
        videoPlayList.Add(Video8);

        VideoEntity.VideoTails Tail1 = new VideoEntity.VideoTails("H_encontra_10.mp4", "somthing");
        VideoEntity.VideoTails Tail2 = new VideoEntity.VideoTails("H_have_fun_10.mp4", "somthing");
        VideoEntity.VideoTails Tail3 = new VideoEntity.VideoTails("H_informa-te_10.mp4", "somthing");
        VideoEntity.VideoTails Tail4 = new VideoEntity.VideoTails("H_move-te_10.mp4", "somthing");
        VideoEntity.VideoTails Tail5 = new VideoEntity.VideoTails("H_selfie_10.mp4", "somthing");

        videoPlayListFill.Add(Tail1);
        videoPlayListFill.Add(Tail2);
        videoPlayListFill.Add(Tail3);
        videoPlayListFill.Add(Tail4);
        videoPlayListFill.Add(Tail5);

        LoadFillVideo();

        videoPlaytimVerification.Tick += new EventHandler(videoPlaytimVerification_Tick);
        videoPlaytimVerification.Interval = new TimeSpan(0, 0, 1);
        videoPlaytimVerification.Start();


    }

    #region METHODS

    /*###############################################*/
    /*###############################################*/
    /*###############################################*/

    private bool SearchVideo(string vidName)
    {
        foreach (var file in directoryInfo.GetFiles())
        {
            if (vidName == file.ToString())
            {
                return true;
            }
        }
        return false;
    }

    /*###############################################*/
    /*###############################################*/
    /*###############################################*/

    void createMediaElement(string videoSource)
    {
        me.LoadedBehavior = MediaState.Manual;
        me.Source = new Uri(DIRECTORY + videoSource, UriKind.Absolute);
        me.Play();
        mediaPlayerGrid.Children.Add(me);
    }

    /*###############################################*/
    /*###############################################*/
    /*###############################################*/


    private void LoadFillVideo()
    {
       log.LogMessage("videoFill");

        Random rand = new Random(DateTime.Now.ToString().GetHashCode());
        randIndex = rand.Next(0, videoPlayListFill.Count);
        try
        {
            mediaPlayerGrid.Children.Remove(me);
            createMediaElement(videoPlayListFill[randIndex].Filename);
            VideoEnd(me, videoPlayListFill[randIndex].Filename);
        }
        catch { }
    }



    /*###############################################*/
    /*###############################################*/
    /*###############################################*/

    private void VideoEnd(MediaElement me, string vidName)
    {
        me.MediaEnded += (a, b) =>
        {
            Console.WriteLine("Passei no evento ended");
            LoadFillVideo();
        };
    }

    /*###############################################*/
    /*###############################################*/
    /*###############################################*/
    #endregion

    #region EVENTS

    private void videoPlaytimVerification_Tick(object sender, EventArgs e)
    {

        Console.WriteLine(seconds);

        foreach (VideoEntity.Video vid in videoPlayList)
        {
            if (vid.Playtime == seconds)
            {
                Console.WriteLine("     " + vid.Filename);

                vidName = vid.Filename;

                try
                {
                    mediaPlayerGrid.Children.Remove(me);
                    createMediaElement(vidName);
                    VideoEnd(me, vidName);
                }
                catch { }

                if (!SearchVideo(vidName))
                {
                    Console.WriteLine("Passei no dispatcher = > loadfill");
                    LoadFillVideo();
                    seconds++;
                    return;
                }
            }
        }

        if (seconds >= 89990)
            seconds = 3599;



        seconds++;
    }

    /*###############################################*/
    /*###############################################*/
    /*###############################################*/



    #endregion

}

当我运行我的代码并尝试关注它时,第一次视频填充结束时,它很好,它只在VideoEnd()传递一次,但第二次,它就像这个LoadFill() - &gt ; VideoEnd() - &gt; LoadFill() - &gt; VideoEnd(),

第三次这样:

LoadFill() - &gt; VideoEnd() - &gt; LoadFill() - &gt; VideoEnd() - &gt; LoadFill() - &gt; VideoEnd() - &gt; LoadFill() - &gt; VideoEnd()

它继续这样,呈指数级增长。知道是什么导致了这个吗?

0 个答案:

没有答案