/* * Ask the server to create a new play for us, and queue it up */ private IEnumerator RequestNextPlay() { if (pendingRequest != null) { // we're already waiting for a play to come in yield break; } yield return StartCoroutine (EnsureClientId ()); while (clientId != null) { Ajax ajax = new Ajax(Ajax.RequestType.POST, apiServerBase + "/play"); ajax.addParameter("formats", formats); ajax.addParameter("client_id", clientId); ajax.addParameter("max_bitrate", maxBitrate); if (!String.IsNullOrEmpty(_placementId)) { ajax.addParameter("placement_id", _placementId); } if (!String.IsNullOrEmpty(_stationId)) { ajax.addParameter ("station_id", _stationId); } // let the rest of the code know we're awaiting a response pendingRequest = new PendingRequest { ajax = ajax, retryCount = 0 }; yield return StartCoroutine(SignedRequest(ajax)); if ((pendingRequest == null) || (pendingRequest.ajax != ajax)) { // another request snuck in while waiting for the response to this one, // so we don't care about this one any more - just quit yield break; } if (ajax.success) { inUS = true; pendingRequest = null; if (current != null) { // play this when the current song is complete pendingPlay = ajax.response["play"]; } else { // start playing this right now, since nothing else is active AssignCurrentPlay (ajax.response["play"]); } yield break; } else if (ajax.error == (int) FeedError.NoMoreMusic) { if (current != null) { // ran out of music to play, but we're still playing something, so // just make a note here pendingPlay = null; } else { // ran out of music, and nothing else to play exhausted = true; if (onPlaysExhausted != null) onPlaysExhausted(this); } pendingRequest = null; yield break; } else if (ajax.error == (int) FeedError.NotUS) { // user isn't in the united states, so can't play anything inUS = false; if (onNotInUS != null) onNotInUS(this); yield break; } else { // some unknown error pendingRequest.retryCount++; // wait for an increasingly long time before retrying yield return new WaitForSeconds(0.5f * (float) Math.Pow(2.0, pendingRequest.retryCount)); } } }
/* * Tell the server how much of the song we've listened to */ public virtual void ReportPlayElapsed(int seconds) { if (current == null) { throw new Exception ("Attempt to report elapsed play time, but the pay hasn't started"); } Ajax ajax = new Ajax (Ajax.RequestType.POST, apiServerBase + "/play/" + current.play ["id"] + "/elapse"); ajax.addParameter ("seconds", seconds.ToString ()); StartCoroutine (SignedRequest (ajax)); }