close

今天測試同步播放兩支影片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Threading;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Windows.Forms;

namespace WaferPositioning
{
    // 同時播放兩支影片
    public class DualVideoPlayer
    {
        private VideoCapture capture1;
        private VideoCapture capture2;
        private ManualResetEvent pauseEvent = new ManualResetEvent(true);
        private bool stopThreads = false;

        public event EventHandler<FrameReadyEventArgs> FrameReady;

        public DualVideoPlayer(string videoFile1, string videoFile2)
        {
            capture1 = new VideoCapture(videoFile1);
            capture2 = new VideoCapture(videoFile2);
        }

        public void Start()
        {
            stopThreads = false;
            pauseEvent.Set(); // Resume if paused
            ThreadPool.QueueUserWorkItem(o => StartVideoPlayback());
        }

        public void Pause()
        {
            pauseEvent.Reset(); // Pause playback
        }

        public void Resume()
        {
            pauseEvent.Set(); // Resume playback
        }

        public void Stop()
        {
            stopThreads = true;
            pauseEvent.Set(); // Resume if paused
        }

        private void StartVideoPlayback()
        {
            while (!stopThreads)
            {
                pauseEvent.WaitOne(); // Pause if signaled

                Mat frame1 = new Mat();
                Mat frame2 = new Mat();

                // Read frames from the video captures
                capture1.Read(frame1);
                capture2.Read(frame2);

                if (!frame1.IsEmpty && !frame2.IsEmpty)
                {
                    FrameReady?.Invoke(this, new FrameReadyEventArgs(frame1, frame2));
                }
                else
                {
                    // End of video, you may want to stop or loop the playback
                    capture1.Set(CapProp.PosFrames, 0);
                    capture2.Set(CapProp.PosFrames, 0);
                }
                Thread.Sleep(33);
                Application.DoEvents(); // Allow UI events to be processed
            }
        }
    }

    public class FrameReadyEventArgs : EventArgs
    {
        public Mat Frame1 { get; }
        public Mat Frame2 { get; }

        public FrameReadyEventArgs(Mat frame1, Mat frame2)
        {
            Frame1 = frame1;
            Frame2 = frame2;
        }
    }
}
arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()