123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- namespace OmxPlayerGui
- {
- public class cPlayer
- {
- private Process _player;
- private const string PlayerPath = "/usr/bin/omxplayer";
- private const int DefaultVolume = 16;
- private int _volume;
- private Action _finishedPlaying;
- public cPlayer ()
- {
- _player = null;
- _volume = DefaultVolume;
- _finishedPlaying = null;
- }
- public void Dispose()
- {
- if (_player != null) {
- _player.Dispose ();
- _player = null;
- }
- return;
- }
- public string[] Modes()
- {
- string[] modeList;
- modeList = new String[4];
- modeList[1] = "OMXPlayer Audio Jack";
- modeList[2] = "OMXPlayer HDMI Port";
- modeList[3] = "OMXPlayer Default Output";
- return modeList;
- }
- public void PlaySong (string song,string mode,Action finishedPlaying)
- {
- ProcessStartInfo myProcessStartInfo;
- Thread waitingThread;
- ThreadStart start;
- string parameters;
- string vol;
- Action WaitingToStop = () => {
- bool exists;
- //bool adjustVolume;
- //int oldVolume;
- try
- {
- //adjustVolume = true;
- while(true)
- {
- Thread.MemoryBarrier();
- exists = ((_player != null) && (_finishedPlaying != null));
- Thread.MemoryBarrier();
- if (!exists)
- {
- break;
- }
- if (_player.WaitForExit (500))
- {
- break;
- }
- /*if (adjustVolume)
- {
- adjustVolume = false;
- oldVolume = _volume;
- _volume = DefaultVolume;
- Volume = oldVolume;
- }*/
- }
- if (_finishedPlaying != null)
- {
- _finishedPlaying ();
- }
- }catch(Exception)
- {
- }
- _player = null;
- waitingThread = null;
- return;
- };
- _finishedPlaying = finishedPlaying;
- _player = new System.Diagnostics.Process ();
- myProcessStartInfo = new System.Diagnostics.ProcessStartInfo (PlayerPath);
- _player.StartInfo = myProcessStartInfo;
- switch (mode) {
- case "OMXPlayer Audio Jack":
- {
- parameters = "-o local {0}\"{1}\"";
- break;
- }
- case "OMXPlayer HDMI Port":
- {
- parameters = "-o hdmi {0}\"{1}\"";
- break;
- }
- default:
- {
- parameters = "{0}\"{1}\"";
- break;
- }
- }
- vol = "";
- if (_volume != DefaultVolume) {
- vol = String.Format ("--vol {0} ", (_volume - DefaultVolume) * 300);
- }
- myProcessStartInfo.Arguments = String.Format (parameters, vol,song);
- myProcessStartInfo.RedirectStandardInput = true;
- myProcessStartInfo.RedirectStandardOutput = true;
- myProcessStartInfo.CreateNoWindow = true;
- myProcessStartInfo.UseShellExecute = false;
- _player.OutputDataReceived += new DataReceivedEventHandler(PlayerOutputHandler);
- _player.Start ();
- _player.BeginOutputReadLine ();
- if (_finishedPlaying != null)
- {
- start = new ThreadStart(WaitingToStop);
- waitingThread = new Thread(start);
- waitingThread.IsBackground = true;
- waitingThread.Priority = ThreadPriority.BelowNormal;
- waitingThread.Start();
- }
- //_player.WaitForExit (100);
- return;
- }
- public int MinVolume()
- {
- return 0;
- }
- public int MaxVolume()
- {
- return 31;
- }
- public int Volume
- {
- get
- {
- return _volume;
- }
- set
- {
- int newVolume = value;
- int currentVolume = _volume;
- while (newVolume > currentVolume) {
- IncreaseVolume ();
- currentVolume++;
- Thread.Sleep (50);
- }
- while (newVolume < currentVolume) {
- DecreaseVolume ();
- currentVolume--;
- Thread.Sleep (50);
- }
- }
- }
- private void PlayerOutputHandler(object sendingProcess
- , DataReceivedEventArgs outLine)
- {
- // Don't show Have a nice day ;)
- return;
- }
- public void Stop()
- {
- if (_player != null) {
- SendKey ('q');
- }
- return;
- }
- public bool IsPlaying()
- {
- return ((_player != null) && (!_player.HasExited));
- }
- public void Pause()
- {
- SendKey (' ');
- return;
- }
- public void UnPause()
- {
- Pause ();
- return;
- }
- public void SkipToTheBeginning() {
- SendHex ("1B5B42");
- return;
- }
- public void IncreaseVolume()
- {
- if (_volume < MaxVolume ()) {
- _volume++;
- SendKey ('+');
- }
- return;
- }
- public void DecreaseVolume()
- {
- if (_volume > MinVolume ()) {
- _volume--;
- SendKey ('-');
- }
- return;
- }
- public void SkipForward()
- {
- // http://subupi.blogspot.com/2012/10/piping-across-shell-sessions-to-control.html
- SendHex ("1B5B43");
- return;
- }
- public void SkipBack()
- {
- SendHex ("1B5B44");
- return;
- }
- /// <summary>
- /// Sends several keys as hex to OmxPlayer.
- /// </summary>
- /// <param name="hex">Hex.</param>
- private void SendHex(string hex)
- {
- // 386-290-9553
- var output = new System.Text.StringBuilder();
- System.Func<char,int> GetHex = (x) => {
- var value = "0123456789ABCDEFabcdef".IndexOf(x);
- if (value > 15) {
- value -= 6;
- }
- return value; // http://subupi.blogspot.com/2012/10/piping-across-shell-sessions-to-control.html
- };
- for (int loop = 0; loop < hex.Length; loop += 2) {
- output.Append((char)(GetHex(hex[loop]) * 16 + GetHex(hex[loop+1])));
- }
- try
- {
- if (_player != null) {
- _player.StandardInput.Write(output.ToString());
- }
- } catch(Exception) {
- //System.Windows.Forms.MessageBox.Show (ex.Message);
- }
- return;
- }
- /// <summary>
- /// Sends one key to OmxPlayer.
- /// </summary>
- /// <param name="key">Key.</param>
- private void SendKey(char key)
- {
- try
- {
- if (_player != null) {
- _player.StandardInput.Write(key);
- }
- } catch(Exception) {
- //System.Windows.Forms.MessageBox.Show (ex.Message);
- }
- return;
- }
- }
- }
|