/// <summary> /// Send a Play request to the DNLA device /// </summary> /// <returns></returns> private async Task <bool> PlaySong() { string soapContent = DlnaRequestHelper.MakeSoapRequest("Play", "<Speed>1</Speed>\r\n"); string request = DlnaRequestHelper.MakeRequest("POST", PlaybackDevice.PlayUrl, "urn:schemas-upnp-org:service:AVTransport:1#Play", PlaybackDevice.IPAddress, PlaybackDevice.Port, soapContent); // Run off the calling thread string response = await Task.Run(() => DlnaRequestHelper.SendRequest(PlaybackDevice, request)); return(DlnaRequestHelper.GetResponseCode(response) == 200); }
/// <summary> /// Send the Uri of the song to the DNLA device /// </summary> /// <param name="fileName"></param> /// <returns></returns> private async Task <bool> PrepareSong(string fileName, Song songToPlay) { string soapContent = DlnaRequestHelper.MakeSoapRequest("SetAVTransportURI", $"<CurrentURI>{fileName}</CurrentURI>\r\n<CurrentURIMetaData>{Desc( fileName, songToPlay )}</CurrentURIMetaData>\r\n"); string request = DlnaRequestHelper.MakeRequest("POST", PlaybackDevice.PlayUrl, "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI", PlaybackDevice.IPAddress, PlaybackDevice.Port, soapContent); // Run off the calling thread string response = await Task.Run(() => DlnaRequestHelper.SendRequest(PlaybackDevice, request)); return(DlnaRequestHelper.GetResponseCode(response) == 200); }
/// <summary> /// Send a Pause request to the DNLA device /// </summary> public async override void Pause() { string request = DlnaRequestHelper.MakeRequest("POST", PlaybackDevice.PlayUrl, "urn:schemas-upnp-org:service:AVTransport:1#Pause", PlaybackDevice.IPAddress, PlaybackDevice.Port, DlnaRequestHelper.MakeSoapRequest("Pause")); // Run off the calling thread string response = await Task.Run(() => DlnaRequestHelper.SendRequest(PlaybackDevice, request)); if (DlnaRequestHelper.GetResponseCode(response) == 200) { IsPlaying = false; ReleaseLock(); } }
/// <summary> /// Send a stop request to the DNLA device /// </summary> public async override void Stop() { // Assume the DLMA request will complte and clear some state variables first IsPlaying = false; ReleaseLock(); string request = DlnaRequestHelper.MakeRequest("POST", PlaybackDevice.PlayUrl, "urn:schemas-upnp-org:service:AVTransport:1#Stop", PlaybackDevice.IPAddress, PlaybackDevice.Port, DlnaRequestHelper.MakeSoapRequest("Stop")); // Run off the calling thread string response = await Task.Run(() => DlnaRequestHelper.SendRequest(PlaybackDevice, request)); }
/// <summary> /// Called when the position timer has elapsed. /// Get the current playback posiotn from the DLNA device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected override async void PositionTimerElapsed() { // Only process the timer if a song is being played if (IsPlaying == true) { // Stop the timer whilst getting the position as it may take a while StopTimer(); // Send the GetPositionInfo request and get the response // Run off the calling thread string response = await Task.Run(() => DlnaRequestHelper.SendRequest(PlaybackDevice, DlnaRequestHelper.MakeRequest("POST", PlaybackDevice.PlayUrl, "urn:schemas-upnp-org:service:AVTransport:1#GetPositionInfo", PlaybackDevice.IPAddress, PlaybackDevice.Port, DlnaRequestHelper.MakeSoapRequest("GetPositionInfo")))); if (DlnaRequestHelper.GetResponseCode(response) == 200) { durationMilliseconds = TimeStringToMilliseconds(response.TrimStart("<TrackDuration>").TrimAfter("</TrackDuration>")); positionMilliseconds = TimeStringToMilliseconds(response.TrimStart("<RelTime>").TrimAfter("</RelTime>")); Logger.Log($"Position: {positionMilliseconds}, Duration {durationMilliseconds}"); // Assume the track has not finished bool nextTrack = false; // If a chnage has already been schedukled then do it now if (changeTrackNextTime == true) { nextTrack = true; changeTrackNextTime = false; } else { // If the duration is 0 this could be due to missing the end of a song, or it can also happen at the // very start of a track. So keep track of this and if it happens a few times switch to the next track if (durationMilliseconds == 0) { if (++noPlayCount > 3) { nextTrack = true; noPlayCount = 0; } } else { noPlayCount = 0; // If the position is within 1/4 second of the duration when assume this track has finished and move on to the next track. // If the position is around 1 second of the duration then move on to the next track the next time this position is obtained int timeLeft = Math.Abs(durationMilliseconds - positionMilliseconds); if (timeLeft < 250) { nextTrack = true; } else if (timeLeft < 1050) { changeTrackNextTime = true; } } } if (nextTrack == true) { IsPlaying = false; ReleaseLock(); ReportSongFinished(); } } else { // Failed to get the response - report this and try again when the timer expires Logger.Error("Failed to get PositionInfo"); } StartTimer(); } base.PositionTimerElapsed(); }