frmSong.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using System;
  2. using System.Windows.Forms;
  3. namespace OmxPlayerGui
  4. {
  5. public class frmSong:Form
  6. {
  7. string _song;
  8. Button btnFirst;
  9. Button btnBack;
  10. Button btnNext;
  11. Button btnPause;
  12. Button btnStop;
  13. Button btnPlay;
  14. Button btnQuit;
  15. Button btnLouder;
  16. Button btnSofter;
  17. Label lblInfo;
  18. Label lblSong;
  19. cPlayer _player;
  20. DateTime? _ignoreButton;
  21. bool _closeWhenSongFinishes; // If the song plays when the program starts and there's no interaction, close when the program stops.
  22. public frmSong (string song)
  23. {
  24. ShouldIgnoreButton = false;
  25. _song = FindSong(song);
  26. InitializeComponent ();
  27. _closeWhenSongFinishes = false;
  28. if (!String.IsNullOrEmpty (_song)) {
  29. _closeWhenSongFinishes = true;
  30. PlaySong ();
  31. }
  32. }
  33. /// <summary>
  34. /// Extracts the song name from the command line.
  35. /// </summary>
  36. /// <returns>The song.</returns>
  37. /// <param name="song">Command line</param>
  38. private string FindSong(string song)
  39. {
  40. int search;
  41. const char DOUBLE_QUOTE = '"';
  42. search = song.IndexOf (".exe ", StringComparison.OrdinalIgnoreCase);
  43. song = song.Substring (search + 5);
  44. // Remove double quotes.
  45. if ((!String.IsNullOrEmpty(song)) && (song[0] == DOUBLE_QUOTE))
  46. {
  47. song = song.Substring (1);
  48. }
  49. if ((!String.IsNullOrEmpty(song)) && (song[song.Length-1] == DOUBLE_QUOTE))
  50. {
  51. song = song.Substring (0, song.Length - 1);
  52. }
  53. //song = "/home/pi/boot.bin/Shared/Mp3/ZZ Top - Cheap Sunglasses.mp3";
  54. return song;
  55. }
  56. private void InitializeComponent ()
  57. {
  58. Width = 440;
  59. Height = 160;
  60. Text = "OmxPlayer " + System.IO.Path.GetFileName(_song);
  61. ControlCreator.Add (Controls, out btnFirst, "btnFirst", "|<", 10, 10, 50, 25);
  62. btnFirst.AccessibleDescription = "Move to the beginning of the song (Up Arrow)";
  63. btnFirst.Click += btnFirst_Click;
  64. AddKeyEvents (btnFirst);
  65. ControlCreator.Add (Controls, out btnBack, "btnBack", "<-", 70, 10,50, 25);
  66. btnBack.AccessibleDescription = "Move back 30 seconds (Left Arrow)";
  67. btnBack.Click += btnBack_Click;
  68. AddKeyEvents (btnBack);
  69. ControlCreator.Add (Controls, out btnPlay, "btnPlay", "|>", 130, 10, 50, 25);
  70. btnPlay.AccessibleDescription = "Play (Space)";
  71. btnPlay.Click += btnPlay_Click;
  72. AddKeyEvents (btnPlay);
  73. ControlCreator.Add (Controls, out btnPause, "btnPause", "||", 200, 10, 50, 25);
  74. btnPause.AccessibleDescription = "Pause (Space)";
  75. btnPause.Click += btnPause_Click;
  76. AddKeyEvents (btnPause);
  77. ControlCreator.Add (Controls, out btnStop, "btnStop", "[]", 270, 10, 50, 25);
  78. btnStop.AccessibleDescription = "Stop (Q)";
  79. btnStop.Click += btnStop_Click;
  80. AddKeyEvents (btnStop);
  81. ControlCreator.Add (Controls, out btnNext, "btnNext", "->", 340, 10, 50, 25);
  82. btnNext.AccessibleDescription = "Move forward 30 seconds (Right Arrow)";
  83. btnNext.Click += btnNext_Click;
  84. AddKeyEvents (btnNext);
  85. ControlCreator.Add (Controls, out btnLouder, "btnLouder","Louder",10, 45, 100, 25);
  86. btnLouder.AccessibleDescription = "Increases volume (+)";
  87. btnLouder.Click += btnLouder_Click;
  88. AddKeyEvents (btnLouder);
  89. ControlCreator.Add (Controls, out btnSofter, "btnSofter", "Softer", 120, 45, 100, 25);
  90. btnSofter.AccessibleDescription = "Decreases volume (-)";
  91. btnSofter.Click += btnSofter_Click;
  92. AddKeyEvents (btnSofter);
  93. ControlCreator.Add (Controls, out btnQuit, "btnQuit", "X", 230, 45, 50, 25);
  94. btnQuit.AccessibleDescription = "Exits the program (Esc)";
  95. btnQuit.Click += btnQuit_Click;
  96. AddKeyEvents (btnQuit);
  97. ControlCreator.Add (Controls, out lblInfo, "lblInfo", "", 10, 75, Width - 20, 25);
  98. AddMouseEvents (btnFirst);
  99. AddMouseEvents (btnBack);
  100. AddMouseEvents (btnPlay);
  101. AddMouseEvents (btnPause);
  102. AddMouseEvents (btnStop);
  103. AddMouseEvents (btnNext);
  104. AddMouseEvents (btnQuit);
  105. AddMouseEvents (btnLouder);
  106. AddMouseEvents (btnSofter);
  107. ControlCreator.Add (Controls, out lblSong, "lblSong", "", 10, 110, Width - 20, 25);
  108. lblSong.Text = _song;
  109. this.FormClosing += Form_Closing;
  110. //this.KeyPress += Form_KeyPress;
  111. //this.PreviewKeyDown += Form_KeyDown;
  112. //this.KeyDown += Form_KeyDown;
  113. //myToolTip = new ToolTip ();
  114. //this.Controls.Add (myToolTip,"myToolTip");
  115. return;
  116. }
  117. void btnSofter_Click (object sender, EventArgs e)
  118. {
  119. if (ShouldIgnoreButton) {
  120. return;
  121. }
  122. if (IsPlaying) {
  123. _player.DecreaseVolume ();
  124. }
  125. }
  126. void btnLouder_Click (object sender, EventArgs e)
  127. {
  128. if (ShouldIgnoreButton) {
  129. return;
  130. }
  131. if (IsPlaying) {
  132. _player.IncreaseVolume ();
  133. }
  134. }
  135. /*protected override bool IsInputChar(char charCode)
  136. {
  137. if (charCode == 32) {
  138. return true;
  139. } else {
  140. return base.IsInputChar (charCode);
  141. }
  142. }
  143. protected override bool IsInputKey(Keys keyData)
  144. {
  145. if (keyData == Keys.Down || keyData == Keys.Up ||
  146. keyData == Keys.Left || keyData == Keys.Right)
  147. {
  148. return true;
  149. }
  150. else
  151. {
  152. return base.IsInputKey(keyData);
  153. }
  154. }*/
  155. private void AddKeyEvents(Button btn)
  156. {
  157. //btn.KeyDown += Form_KeyDown;
  158. //btn.KeyPress += Form_KeyPress;
  159. btn.PreviewKeyDown += Form_PreviewKeyDown;
  160. }
  161. private void btnBack_Click(Object sender,EventArgs e)
  162. {
  163. if (ShouldIgnoreButton) {
  164. return;
  165. }
  166. _closeWhenSongFinishes = false;
  167. if (IsPlaying) {
  168. _player.SkipBack ();
  169. }
  170. }
  171. private void btnNext_Click(Object sender,EventArgs e)
  172. {
  173. if (ShouldIgnoreButton) {
  174. return;
  175. }
  176. _closeWhenSongFinishes = false;
  177. if (IsPlaying) {
  178. _player.SkipForward ();
  179. }
  180. }
  181. private void Form_PreviewKeyDown(Object sender,PreviewKeyDownEventArgs e)
  182. {
  183. try
  184. {
  185. switch (e.KeyCode) {
  186. case Keys.Right:
  187. {
  188. _closeWhenSongFinishes = false;
  189. if (IsPlaying) {
  190. _player.SkipForward ();
  191. e.IsInputKey = true;
  192. }
  193. break;
  194. }
  195. case Keys.Left:
  196. {
  197. _closeWhenSongFinishes = false;
  198. if (IsPlaying) {
  199. _player.SkipBack ();
  200. e.IsInputKey = true;
  201. }
  202. break;
  203. }
  204. case Keys.Q:
  205. {
  206. e.IsInputKey = true;
  207. if (IsPlaying)
  208. {
  209. _closeWhenSongFinishes = false;
  210. Stop();
  211. }
  212. break;
  213. }
  214. case Keys.Escape:
  215. {
  216. e.IsInputKey = true;
  217. if (IsPlaying) {
  218. _closeWhenSongFinishes = false;
  219. Stop ();
  220. }
  221. Close ();
  222. break;
  223. }
  224. case Keys.Space:
  225. {
  226. e.IsInputKey = true;
  227. ShouldIgnoreButton = true;
  228. if (IsPlaying) {
  229. _player.Pause ();
  230. } else {
  231. PlaySong ();
  232. }
  233. break;
  234. }
  235. case Keys.OemMinus:
  236. case Keys.VolumeDown:
  237. {
  238. e.IsInputKey = true;
  239. if (IsPlaying) {
  240. _player.DecreaseVolume ();
  241. }
  242. break;
  243. }
  244. case Keys.Oemplus:
  245. case Keys.VolumeUp:
  246. {
  247. e.IsInputKey = true;
  248. if (IsPlaying) {
  249. _player.IncreaseVolume ();
  250. }
  251. break;
  252. }
  253. case Keys.VolumeMute:
  254. {
  255. // Not implemented.
  256. break;
  257. }
  258. case Keys.Up:
  259. {
  260. e.IsInputKey = true;
  261. if (IsPlaying) {
  262. _player.SkipToTheBeginning ();
  263. }
  264. break;
  265. }
  266. case Keys.Down:
  267. {
  268. e.IsInputKey = true;
  269. Close ();
  270. break;
  271. }
  272. }
  273. } catch(Exception) {
  274. }
  275. return;
  276. }
  277. private bool IsPlaying
  278. {
  279. get {
  280. return ((_player != null) && (_player.IsPlaying ()));
  281. }
  282. }
  283. private void btnPause_Click(Object sender,EventArgs e)
  284. {
  285. if (ShouldIgnoreButton) {
  286. return;
  287. }
  288. if (IsPlaying)
  289. {
  290. _player.Pause ();
  291. }
  292. }
  293. private void btnQuit_Click(Object sender,EventArgs e)
  294. {
  295. try
  296. {
  297. if (ShouldIgnoreButton) {
  298. return;
  299. }
  300. _closeWhenSongFinishes = false;
  301. Stop ();
  302. Close ();
  303. } catch(Exception) {
  304. }
  305. }
  306. private void Form_Closing(Object sender,FormClosingEventArgs e)
  307. {
  308. try
  309. {
  310. Stop ();
  311. } catch(Exception) {
  312. }
  313. }
  314. private void btnFirst_Click(Object sender,EventArgs e)
  315. {
  316. try
  317. {
  318. if (ShouldIgnoreButton) {
  319. return;
  320. }
  321. if (IsPlaying) {
  322. _player.SkipToTheBeginning ();
  323. }
  324. } catch(Exception) {
  325. }
  326. return;
  327. }
  328. private void btnPlay_Click(Object sender,EventArgs e)
  329. {
  330. try
  331. {
  332. if (ShouldIgnoreButton) {
  333. return;
  334. }
  335. _closeWhenSongFinishes = false;
  336. PlaySong ();
  337. } catch(Exception) {
  338. }
  339. return;
  340. }
  341. private void PlaySong()
  342. {
  343. if (_player == null) {
  344. _player = new cPlayer ();
  345. _player.PlaySong (_song, "OMXPlayer Audio Jack", FinishedPlaying);
  346. } else {
  347. _player.Pause ();
  348. }
  349. return;
  350. }
  351. private void btnStop_Click(Object sender,EventArgs e)
  352. {
  353. try
  354. {
  355. if (ShouldIgnoreButton) {
  356. return;
  357. }
  358. _closeWhenSongFinishes = false;
  359. Stop ();
  360. } catch(Exception) {
  361. }
  362. }
  363. private void Stop()
  364. {
  365. if (IsPlaying) {
  366. _player.Stop ();
  367. _player = null;
  368. }
  369. return;
  370. }
  371. private void FinishedPlaying()
  372. {
  373. _player = null;
  374. if (_closeWhenSongFinishes) {
  375. Close ();
  376. }
  377. }
  378. private void AddMouseEvents(Button buttonToAddEventsTo)
  379. {
  380. buttonToAddEventsTo.MouseEnter += MouseEnter_Handler;
  381. buttonToAddEventsTo.MouseLeave += MouseLeave_Handler;
  382. return;
  383. }
  384. private void MouseEnter_Handler(Object sender,EventArgs e)
  385. {
  386. Button mouseButton = (Button)sender;
  387. lblInfo.Text = mouseButton.AccessibleDescription;
  388. }
  389. private void MouseLeave_Handler(Object sender,EventArgs e)
  390. {
  391. lblInfo.Text = "";
  392. }
  393. private bool ShouldIgnoreButton
  394. {
  395. get {
  396. if (_ignoreButton.HasValue) {
  397. if (DateTime.Now > _ignoreButton.Value) {
  398. _ignoreButton = null;
  399. }
  400. }
  401. return (_ignoreButton.HasValue);
  402. }
  403. set {
  404. if (value) {
  405. // Ignore all buttons for the next 300 miliseconds.
  406. _ignoreButton = DateTime.Now.AddMilliseconds (300);
  407. } else {
  408. _ignoreButton = null;
  409. }
  410. }
  411. }
  412. }
  413. }