// Process Zoom player TCP control commands private void StartPlayPositionRetrieval(string fileName, bool firstStart = false, bool scrobbleEnabled = false) { double postitionTimeCodeMs = 0; try { if (firstStart) { // Wait for process to start and skip short playbacks var startupDelay = TimeSpan.FromSeconds(5); Thread.Sleep(startupDelay); // Check if still running if (!IsPlaying) { Debug.WriteLine("Zoom player - After waiting 5 seconds ZP was no longer running, stopping playback record."); return; } } var tc = new ZPConnection("localhost", _tcpControlPort); var playerInactive = false; char[] charSeparator = {' '}; var videoDuration = ""; // Disable live reporting if (tc.IsConnected) { tc.Write("1100 0" + Environment.NewLine); } else { Debug.WriteLine("Zoom player - No connection could be made to zoom player API using port 4769 to store time code!"); return; } while (tc.IsConnected && IsPlaying) { try { // Check if playing tc.Write("1000" + Environment.NewLine); var playing = tc.Read(); if (playing.StartsWith("1000 ")) { if (playing.Contains(" ")) { var playingSplit = playing.Split(charSeparator); playing = playingSplit[1].Trim(); if (playing != "3") { playerInactive = true; //Debug.WriteLine("Zoom player is paused"); } else { playerInactive = false; //Debug.WriteLine("Zoom player is running"); } } } Thread.Sleep(100); if (!playerInactive) { tc.Write("1110" + Environment.NewLine); videoDuration = tc.Read(); if (string.IsNullOrEmpty(videoDuration.Trim()) || videoDuration.ToLower().Contains("seek") || videoDuration.ToLower().StartsWith("2200") || videoDuration.ToLower().Contains("derived")) { continue; } Thread.Sleep(100); tc.Write("1120" + Environment.NewLine); var timecode = tc.Read(); if (timecode.StartsWith("1120 ")) { try { if (videoDuration.Contains(" ")) { var videoDurationSplit = videoDuration.Split(charSeparator); var timeCodeSplit = timecode.Split(charSeparator); videoDuration = videoDurationSplit[1].Trim(); timecode = timeCodeSplit[1].Trim(); double videoDurationMS = 0; var successfullyParsedVideoDuration = double.TryParse(videoDuration, out videoDurationMS); var successfullyParsedTimeCode = double.TryParse(timecode, out postitionTimeCodeMs); if (successfullyParsedVideoDuration && successfullyParsedTimeCode) { tc.Write("1800" + Environment.NewLine); var videoFilenameRetrieved = tc.Read(); if (videoFilenameRetrieved.StartsWith("1800 ")) { var videoFilenameReadOut = videoFilenameRetrieved.Replace("1800 ", string.Empty).Trim(); if (!string.IsNullOrEmpty(videoFilenameReadOut) && videoFilenameReadOut.ToLower() != fileName.ToLower()) { Debug.WriteLine("Zoom player - Video filename didn't match original: " + videoFilenameReadOut); return; } } } } } catch (Exception) { } } } } catch (Exception e) { Thread.Sleep(1000); if (!IsPlaying) { Thread.Sleep(250); Debug.WriteLine("[Position retrieval] Zoom player - no longer running"); UpdateFilePosition(fileName, postitionTimeCodeMs); } else { Thread.Sleep(250); Debug.WriteLine("[Position retrieval] Zoom player - error occured"); Debug.WriteLine(e.Message); StartPlayPositionRetrieval(fileName); } } // Keep updating positions if we have scrobble enabled if (scrobbleEnabled) { UpdateFilePosition(fileName, postitionTimeCodeMs); } } UpdateFilePosition(fileName, postitionTimeCodeMs); } catch (Exception) { Thread.Sleep(1000); if (!IsPlaying) { UpdateFilePosition(fileName, postitionTimeCodeMs); } else { StartPlayPositionRetrieval(fileName, false, scrobbleEnabled); } } }
// Process Zoom player TCP control commands private void StartPlayPositionRetrieval(string fileName, bool firstStart = false, bool scrobbleEnabled = false) { double postitionTimeCodeMs = 0; try { if (firstStart) { // Wait for process to start and skip short playbacks var startupDelay = TimeSpan.FromSeconds(5); Thread.Sleep(startupDelay); // Check if still running if (!IsPlaying) { Debug.WriteLine("Zoom player - After waiting 5 seconds ZP was no longer running, stopping playback record."); return; } } var tc = new ZPConnection("localhost", _tcpControlPort); var playerInactive = false; char[] charSeparator = { ' ' }; var videoDuration = ""; // Disable live reporting if (tc.IsConnected) { tc.Write("1100 0" + Environment.NewLine); } else { Debug.WriteLine("Zoom player - No connection could be made to zoom player API using port 4769 to store time code!"); return; } while (tc.IsConnected && IsPlaying) { try { // Check if playing tc.Write("1000" + Environment.NewLine); var playing = tc.Read(); if (playing.StartsWith("1000 ")) { if (playing.Contains(" ")) { var playingSplit = playing.Split(charSeparator); playing = playingSplit[1].Trim(); if (playing != "3") { playerInactive = true; //Debug.WriteLine("Zoom player is paused"); } else { playerInactive = false; //Debug.WriteLine("Zoom player is running"); } } } Thread.Sleep(100); if (!playerInactive) { tc.Write("1110" + Environment.NewLine); videoDuration = tc.Read(); if (string.IsNullOrEmpty(videoDuration.Trim()) || videoDuration.ToLower().Contains("seek") || videoDuration.ToLower().StartsWith("2200") || videoDuration.ToLower().Contains("derived")) { continue; } Thread.Sleep(100); tc.Write("1120" + Environment.NewLine); var timecode = tc.Read(); if (timecode.StartsWith("1120 ")) { try { if (videoDuration.Contains(" ")) { var videoDurationSplit = videoDuration.Split(charSeparator); var timeCodeSplit = timecode.Split(charSeparator); videoDuration = videoDurationSplit[1].Trim(); timecode = timeCodeSplit[1].Trim(); double videoDurationMS = 0; var successfullyParsedVideoDuration = double.TryParse(videoDuration, out videoDurationMS); var successfullyParsedTimeCode = double.TryParse(timecode, out postitionTimeCodeMs); if (successfullyParsedVideoDuration && successfullyParsedTimeCode) { tc.Write("1800" + Environment.NewLine); var videoFilenameRetrieved = tc.Read(); if (videoFilenameRetrieved.StartsWith("1800 ")) { var videoFilenameReadOut = videoFilenameRetrieved.Replace("1800 ", string.Empty).Trim(); if (!string.IsNullOrEmpty(videoFilenameReadOut) && videoFilenameReadOut.ToLower() != fileName.ToLower()) { Debug.WriteLine("Zoom player - Video filename didn't match original: " + videoFilenameReadOut); return; } } } } } catch (Exception) { } } } } catch (Exception e) { Thread.Sleep(1000); if (!IsPlaying) { Thread.Sleep(250); Debug.WriteLine("[Position retrieval] Zoom player - no longer running"); UpdateFilePosition(fileName, postitionTimeCodeMs); } else { Thread.Sleep(250); Debug.WriteLine("[Position retrieval] Zoom player - error occured"); Debug.WriteLine(e.Message); StartPlayPositionRetrieval(fileName); } } // Keep updating positions if we have scrobble enabled if (scrobbleEnabled) { UpdateFilePosition(fileName, postitionTimeCodeMs); } } UpdateFilePosition(fileName, postitionTimeCodeMs); } catch (Exception) { Thread.Sleep(1000); if (!IsPlaying) { UpdateFilePosition(fileName, postitionTimeCodeMs); } else { StartPlayPositionRetrieval(fileName, false, scrobbleEnabled); } } }