/* * 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)); } } }
/* * Start pulling in music */ public virtual void Tune() { if (string.IsNullOrEmpty(token)) { throw new Exception("no <token> value specified!"); } if (string.IsNullOrEmpty(secret)) { throw new Exception(" no <secret> value specified!"); } // abort any pending requests or plays pendingRequest = null; pendingPlay = null; // pretend we've got music available exhausted = false; // no music has started yet startedPlayback = false; // stop playback of current song and set status to waiting AssignCurrentPlay(null, true); // do some async shizzle StartCoroutine (TuneCoroutine ()); }