CameraVideoPlay.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.ImageCollect
  11. {
  12. public class CameraVideoPlay : Form
  13. {
  14. //播放状态
  15. private const int VLC_STOP = 1;
  16. private const int VLC_PLAY = 2;
  17. private const int VLC_PAUSE = 3;
  18. private int m_stat;
  19. VLCPlayer vlc_player;
  20. public string videoPath = "";
  21. public CameraVideoPlay()
  22. {
  23. InitializeComponent();
  24. InitializeLanguageText();
  25. }
  26. private void CameraVideoPlay_Load(object sender, EventArgs e)
  27. {
  28. IntPtr render_wnd = this.panel1.Handle;
  29. vlc_player = new VLCPlayer(render_wnd);
  30. tbVideoTime.Text = "00:00 000/00:00 000";
  31. m_stat = VLC_STOP;
  32. vlc_player.playLocalVideo(videoPath);
  33. vlc_player.SetPlayTime(0);
  34. vlc_player.Stop();
  35. trackBar1.SetRange(0, (int)(vlc_player.Duration / 100));
  36. trackBar1.Value = 0;
  37. tbVideoTime.Text = string.Format("{0}/{1}", GetTimeString(trackBar1.Value), GetTimeString(trackBar1.Maximum));
  38. }
  39. public double GetPlayTime()
  40. {
  41. return vlc_player.GetPlayTime();
  42. }
  43. private void btnStart_Click(object sender, EventArgs e)
  44. {
  45. if (m_stat == VLC_STOP)
  46. {
  47. trackBar1.SetRange(0, (int)(vlc_player.Duration / 100));
  48. trackBar1.Value = 0;
  49. vlc_player.playLocalVideo(videoPath);
  50. vlc_player.SetPlayTime(0);
  51. vlc_player.Play();
  52. timer1.Start();
  53. m_stat = VLC_PLAY;
  54. }
  55. else if (m_stat == VLC_PLAY)
  56. {
  57. vlc_player.Pause();
  58. timer1.Stop();
  59. m_stat = VLC_PAUSE;
  60. }
  61. else if (m_stat == VLC_PAUSE)
  62. {
  63. vlc_player.Play();
  64. timer1.Start();
  65. m_stat = VLC_PLAY;
  66. }
  67. updateUI();
  68. }
  69. private void btnReset_Click(object sender, EventArgs e)
  70. {
  71. vlc_player.Stop();
  72. m_stat = VLC_STOP;
  73. updateUI();
  74. }
  75. private void timer1_Tick(object sender, EventArgs e)
  76. {
  77. if (m_stat == VLC_PLAY)
  78. {
  79. if (trackBar1.Value == trackBar1.Maximum)
  80. {
  81. vlc_player.Stop();
  82. timer1.Stop();
  83. m_stat = VLC_STOP;
  84. }
  85. else
  86. {
  87. trackBar1.Value = trackBar1.Value + 1;
  88. tbVideoTime.Text = string.Format("{0}/{1}",
  89. GetTimeString(trackBar1.Value),
  90. GetTimeString(trackBar1.Maximum));
  91. }
  92. }
  93. updateUI();
  94. }
  95. private string GetTimeString(int val)
  96. {
  97. val = val * 100;
  98. int msec = val % 1000;
  99. int minute = val / 1000 / 60;
  100. int second = val / 1000 % 60;
  101. return string.Format("{0:00}:{1:00} {2:000}", minute, second, msec);
  102. }
  103. private void updateUI()
  104. {
  105. if (m_stat == VLC_PLAY)
  106. {
  107. btnStart.Enabled = true;
  108. btnReset.Enabled = true;
  109. btnStart.Text = PdnResources.GetString("Menu.suspended.Text");
  110. }
  111. else
  112. {
  113. btnStart.Enabled = true;
  114. btnReset.Enabled = false;
  115. btnStart.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
  116. }
  117. }
  118. private string convertToTimeString(long time)
  119. {
  120. string ms = (time % 1000).ToString();
  121. if (ms.Length < 2)
  122. {
  123. ms = "00" + ms;
  124. }
  125. else
  126. {
  127. ms = "0" + ms;
  128. }
  129. string mm = (time / 1000 / 60).ToString();
  130. if (mm.Length < 2)
  131. {
  132. mm = "0" + mm;
  133. }
  134. string ss = ((time / 1000) % 60).ToString();
  135. if (ss.Length < 2)
  136. {
  137. ss = "0" + ss;
  138. }
  139. return mm + ":" + ss + " " + ms;
  140. }
  141. private void trackBar1_Scroll(object sender, EventArgs e)
  142. {
  143. vlc_player.SetPlayTime(trackBar1.Value);
  144. //trackBar1.Value = (int)vlc_player.GetPlayTimeMSec()/100;
  145. }
  146. private void CameraVideoPlay_FormClosing(object sender, FormClosingEventArgs e)
  147. {
  148. if (m_stat != VLC_STOP)
  149. {
  150. timer1.Stop();
  151. }
  152. timer1 = null;
  153. vlc_player.release();
  154. }
  155. private void btnClose_Click(object sender, EventArgs e)
  156. {
  157. this.Close();
  158. }
  159. #region 控件
  160. /// <summary>
  161. /// Required designer variable.
  162. /// </summary>
  163. private System.ComponentModel.IContainer components = null;
  164. /// <summary>
  165. /// Clean up any resources being used.
  166. /// </summary>
  167. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  168. protected override void Dispose(bool disposing)
  169. {
  170. if (disposing && (components != null))
  171. {
  172. components.Dispose();
  173. }
  174. base.Dispose(disposing);
  175. }
  176. // Windows Form Designer generated code
  177. private void InitializeLanguageText()
  178. {
  179. this.btnReset.Text = PdnResources.GetString("Menu.stop.text");
  180. this.btnStart.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
  181. this.btnClose.Text = PdnResources.GetString("Menu.File.Close.Text");
  182. this.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
  183. }
  184. /// <summary>
  185. /// Required method for Designer support - do not modify
  186. /// the contents of this method with the code editor.
  187. /// </summary>
  188. private void InitializeComponent()
  189. {
  190. this.components = new System.ComponentModel.Container();
  191. this.timer1 = new System.Windows.Forms.Timer(this.components);
  192. this.panel2 = new System.Windows.Forms.Panel();
  193. this.tbVideoTime = new System.Windows.Forms.TextBox();
  194. this.trackBar1 = new System.Windows.Forms.TrackBar();
  195. this.btnReset = new System.Windows.Forms.Button();
  196. this.btnStart = new System.Windows.Forms.Button();
  197. this.panel1 = new System.Windows.Forms.Panel();
  198. this.btnClose = new System.Windows.Forms.Button();
  199. this.panel2.SuspendLayout();
  200. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
  201. this.SuspendLayout();
  202. //
  203. // timer1
  204. //
  205. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  206. //
  207. // panel2
  208. //
  209. this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  210. | System.Windows.Forms.AnchorStyles.Right)));
  211. this.panel2.Controls.Add(this.btnClose);
  212. this.panel2.Controls.Add(this.tbVideoTime);
  213. this.panel2.Controls.Add(this.trackBar1);
  214. this.panel2.Controls.Add(this.btnReset);
  215. this.panel2.Controls.Add(this.btnStart);
  216. this.panel2.Location = new System.Drawing.Point(10, 518);
  217. this.panel2.Margin = new System.Windows.Forms.Padding(4);
  218. this.panel2.Name = "panel2";
  219. this.panel2.Size = new System.Drawing.Size(650, 86);
  220. this.panel2.TabIndex = 3;
  221. //
  222. // tbVideoTime
  223. //
  224. this.tbVideoTime.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  225. this.tbVideoTime.BorderStyle = System.Windows.Forms.BorderStyle.None;
  226. this.tbVideoTime.Location = new System.Drawing.Point(488, 8);
  227. this.tbVideoTime.Margin = new System.Windows.Forms.Padding(4);
  228. this.tbVideoTime.Name = "tbVideoTime";
  229. this.tbVideoTime.ReadOnly = true;
  230. this.tbVideoTime.Size = new System.Drawing.Size(162, 18);
  231. this.tbVideoTime.TabIndex = 6;
  232. this.tbVideoTime.Text = "00:00 000/00:00 000";
  233. //
  234. // trackBar1
  235. //
  236. this.trackBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  237. | System.Windows.Forms.AnchorStyles.Right)));
  238. this.trackBar1.AutoSize = false;
  239. this.trackBar1.Location = new System.Drawing.Point(11, 4);
  240. this.trackBar1.Margin = new System.Windows.Forms.Padding(4);
  241. this.trackBar1.Maximum = 999999999;
  242. this.trackBar1.Name = "trackBar1";
  243. this.trackBar1.Size = new System.Drawing.Size(476, 31);
  244. this.trackBar1.TabIndex = 3;
  245. this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
  246. this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
  247. //
  248. // btnReset
  249. //
  250. this.btnReset.Enabled = false;
  251. this.btnReset.Location = new System.Drawing.Point(118, 34);
  252. this.btnReset.Margin = new System.Windows.Forms.Padding(4);
  253. this.btnReset.Name = "btnReset";
  254. this.btnReset.Size = new System.Drawing.Size(98, 42);
  255. this.btnReset.TabIndex = 2;
  256. this.btnReset.UseVisualStyleBackColor = true;
  257. this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
  258. //
  259. // btnStart
  260. //
  261. this.btnStart.Location = new System.Drawing.Point(11, 34);
  262. this.btnStart.Margin = new System.Windows.Forms.Padding(4);
  263. this.btnStart.Name = "btnStart";
  264. this.btnStart.Size = new System.Drawing.Size(98, 42);
  265. this.btnStart.TabIndex = 0;
  266. this.btnStart.UseVisualStyleBackColor = true;
  267. this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
  268. //
  269. // panel1
  270. //
  271. this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  272. | System.Windows.Forms.AnchorStyles.Left)
  273. | System.Windows.Forms.AnchorStyles.Right)));
  274. this.panel1.BackColor = System.Drawing.Color.Black;
  275. this.panel1.Location = new System.Drawing.Point(10, 10);
  276. this.panel1.Margin = new System.Windows.Forms.Padding(4);
  277. this.panel1.Name = "panel1";
  278. this.panel1.Size = new System.Drawing.Size(642, 464);
  279. this.panel1.TabIndex = 2;
  280. //
  281. // btnClose
  282. //
  283. this.btnClose.Location = new System.Drawing.Point(225, 34);
  284. this.btnClose.Name = "btnClose";
  285. this.btnClose.Size = new System.Drawing.Size(98, 42);
  286. this.btnClose.TabIndex = 7;
  287. this.btnClose.UseVisualStyleBackColor = true;
  288. this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
  289. //
  290. // CameraVideoPlay
  291. //
  292. this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
  293. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  294. this.ClientSize = new System.Drawing.Size(665, 614);
  295. this.Controls.Add(this.panel2);
  296. this.Controls.Add(this.panel1);
  297. this.MaximizeBox = false;
  298. this.MinimizeBox = false;
  299. this.Name = "CameraVideoPlay";
  300. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraVideoPlay_FormClosing);
  301. this.Load += new System.EventHandler(this.CameraVideoPlay_Load);
  302. this.panel2.ResumeLayout(false);
  303. this.panel2.PerformLayout();
  304. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
  305. this.ResumeLayout(false);
  306. }
  307. private System.Windows.Forms.Timer timer1;
  308. private System.Windows.Forms.Panel panel2;
  309. private System.Windows.Forms.TextBox tbVideoTime;
  310. private System.Windows.Forms.TrackBar trackBar1;
  311. private System.Windows.Forms.Button btnReset;
  312. private System.Windows.Forms.Button btnStart;
  313. private System.Windows.Forms.Panel panel1;
  314. private System.Windows.Forms.Button btnClose;
  315. #endregion
  316. }
  317. }