cPlayer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace OmxPlayerGui
  5. {
  6. public class cPlayer
  7. {
  8. private Process _player;
  9. private const string PlayerPath = "/usr/bin/omxplayer";
  10. private const int DefaultVolume = 16;
  11. private int _volume;
  12. private Action _finishedPlaying;
  13. public cPlayer ()
  14. {
  15. _player = null;
  16. _volume = DefaultVolume;
  17. _finishedPlaying = null;
  18. }
  19. public void Dispose()
  20. {
  21. if (_player != null) {
  22. _player.Dispose ();
  23. _player = null;
  24. }
  25. return;
  26. }
  27. public string[] Modes()
  28. {
  29. string[] modeList;
  30. modeList = new String[4];
  31. modeList[1] = "OMXPlayer Audio Jack";
  32. modeList[2] = "OMXPlayer HDMI Port";
  33. modeList[3] = "OMXPlayer Default Output";
  34. return modeList;
  35. }
  36. public void PlaySong (string song,string mode,Action finishedPlaying)
  37. {
  38. ProcessStartInfo myProcessStartInfo;
  39. Thread waitingThread;
  40. ThreadStart start;
  41. string parameters;
  42. string vol;
  43. Action WaitingToStop = () => {
  44. bool exists;
  45. //bool adjustVolume;
  46. //int oldVolume;
  47. try
  48. {
  49. //adjustVolume = true;
  50. while(true)
  51. {
  52. Thread.MemoryBarrier();
  53. exists = ((_player != null) && (_finishedPlaying != null));
  54. Thread.MemoryBarrier();
  55. if (!exists)
  56. {
  57. break;
  58. }
  59. if (_player.WaitForExit (500))
  60. {
  61. break;
  62. }
  63. /*if (adjustVolume)
  64. {
  65. adjustVolume = false;
  66. oldVolume = _volume;
  67. _volume = DefaultVolume;
  68. Volume = oldVolume;
  69. }*/
  70. }
  71. if (_finishedPlaying != null)
  72. {
  73. _finishedPlaying ();
  74. }
  75. }catch(Exception)
  76. {
  77. }
  78. _player = null;
  79. waitingThread = null;
  80. return;
  81. };
  82. _finishedPlaying = finishedPlaying;
  83. _player = new System.Diagnostics.Process ();
  84. myProcessStartInfo = new System.Diagnostics.ProcessStartInfo (PlayerPath);
  85. _player.StartInfo = myProcessStartInfo;
  86. switch (mode) {
  87. case "OMXPlayer Audio Jack":
  88. {
  89. parameters = "-o local {0}\"{1}\"";
  90. break;
  91. }
  92. case "OMXPlayer HDMI Port":
  93. {
  94. parameters = "-o hdmi {0}\"{1}\"";
  95. break;
  96. }
  97. default:
  98. {
  99. parameters = "{0}\"{1}\"";
  100. break;
  101. }
  102. }
  103. vol = "";
  104. if (_volume != DefaultVolume) {
  105. vol = String.Format ("--vol {0} ", (_volume - DefaultVolume) * 300);
  106. }
  107. myProcessStartInfo.Arguments = String.Format (parameters, vol,song);
  108. myProcessStartInfo.RedirectStandardInput = true;
  109. myProcessStartInfo.RedirectStandardOutput = true;
  110. myProcessStartInfo.CreateNoWindow = true;
  111. myProcessStartInfo.UseShellExecute = false;
  112. _player.OutputDataReceived += new DataReceivedEventHandler(PlayerOutputHandler);
  113. _player.Start ();
  114. _player.BeginOutputReadLine ();
  115. if (_finishedPlaying != null)
  116. {
  117. start = new ThreadStart(WaitingToStop);
  118. waitingThread = new Thread(start);
  119. waitingThread.IsBackground = true;
  120. waitingThread.Priority = ThreadPriority.BelowNormal;
  121. waitingThread.Start();
  122. }
  123. //_player.WaitForExit (100);
  124. return;
  125. }
  126. public int MinVolume()
  127. {
  128. return 0;
  129. }
  130. public int MaxVolume()
  131. {
  132. return 31;
  133. }
  134. public int Volume
  135. {
  136. get
  137. {
  138. return _volume;
  139. }
  140. set
  141. {
  142. int newVolume = value;
  143. int currentVolume = _volume;
  144. while (newVolume > currentVolume) {
  145. IncreaseVolume ();
  146. currentVolume++;
  147. Thread.Sleep (50);
  148. }
  149. while (newVolume < currentVolume) {
  150. DecreaseVolume ();
  151. currentVolume--;
  152. Thread.Sleep (50);
  153. }
  154. }
  155. }
  156. private void PlayerOutputHandler(object sendingProcess
  157. , DataReceivedEventArgs outLine)
  158. {
  159. // Don't show Have a nice day ;)
  160. return;
  161. }
  162. public void Stop()
  163. {
  164. if (_player != null) {
  165. SendKey ('q');
  166. }
  167. return;
  168. }
  169. public bool IsPlaying()
  170. {
  171. return ((_player != null) && (!_player.HasExited));
  172. }
  173. public void Pause()
  174. {
  175. SendKey (' ');
  176. return;
  177. }
  178. public void UnPause()
  179. {
  180. Pause ();
  181. return;
  182. }
  183. public void SkipToTheBeginning() {
  184. SendHex ("1B5B42");
  185. return;
  186. }
  187. public void IncreaseVolume()
  188. {
  189. if (_volume < MaxVolume ()) {
  190. _volume++;
  191. SendKey ('+');
  192. }
  193. return;
  194. }
  195. public void DecreaseVolume()
  196. {
  197. if (_volume > MinVolume ()) {
  198. _volume--;
  199. SendKey ('-');
  200. }
  201. return;
  202. }
  203. public void SkipForward()
  204. {
  205. // http://subupi.blogspot.com/2012/10/piping-across-shell-sessions-to-control.html
  206. SendHex ("1B5B43");
  207. return;
  208. }
  209. public void SkipBack()
  210. {
  211. SendHex ("1B5B44");
  212. return;
  213. }
  214. /// <summary>
  215. /// Sends several keys as hex to OmxPlayer.
  216. /// </summary>
  217. /// <param name="hex">Hex.</param>
  218. private void SendHex(string hex)
  219. {
  220. // 386-290-9553
  221. var output = new System.Text.StringBuilder();
  222. System.Func<char,int> GetHex = (x) => {
  223. var value = "0123456789ABCDEFabcdef".IndexOf(x);
  224. if (value > 15) {
  225. value -= 6;
  226. }
  227. return value; // http://subupi.blogspot.com/2012/10/piping-across-shell-sessions-to-control.html
  228. };
  229. for (int loop = 0; loop < hex.Length; loop += 2) {
  230. output.Append((char)(GetHex(hex[loop]) * 16 + GetHex(hex[loop+1])));
  231. }
  232. try
  233. {
  234. if (_player != null) {
  235. _player.StandardInput.Write(output.ToString());
  236. }
  237. } catch(Exception) {
  238. //System.Windows.Forms.MessageBox.Show (ex.Message);
  239. }
  240. return;
  241. }
  242. /// <summary>
  243. /// Sends one key to OmxPlayer.
  244. /// </summary>
  245. /// <param name="key">Key.</param>
  246. private void SendKey(char key)
  247. {
  248. try
  249. {
  250. if (_player != null) {
  251. _player.StandardInput.Write(key);
  252. }
  253. } catch(Exception) {
  254. //System.Windows.Forms.MessageBox.Show (ex.Message);
  255. }
  256. return;
  257. }
  258. }
  259. }