Connecting to a sound zone
To stream music, the SDK must first be connected to a specific sound zone within a Soundtrack location. Connecting uses a temporary connect code, generated on demand by Soundtrack. Once connected, the SDK returns a unique Device ID, which your updater sends when checking for Player Library updates.
For implementation details, see splayer_auth_api-1.h. To try out connecting, you'll first need a Soundtrack test account from us; email sdk@soundtrack.io if you don't have one.
Obtaining a connect code
There are two ways to get a connect code, redirect and callback or manual entry. Pass the code to initiate_pair_with_code or pair_with_code_sync.
Redirect and callback (preferred)
This method gives the smoothest user experience because the user doesn't type a code. Your companion application starts the flow, and the player is connected automatically. This is how it works:
- Your companion application initiates a redirect to the following URL:
https://app.soundtrack.io/connect/generic?redirect=<REDIRECT_URL>. Replace<REDIRECT_URL>with the URL of your companion application that will handle the callback. - The user logs in to their Soundtrack account and selects the sound zone to connect the player to.
- Soundtrack redirects the user back to your
<REDIRECT_URL>with the connect code appended as a query parameter:<REDIRECT_URL>?code=<CODE> - Your companion application extracts the
codeparameter and passes it to the SDK to complete the connection.
To simulate this flow, run the following example locally:
#!/usr/bin/env python3
import http.server
import socketserver
import webbrowser
from urllib.parse import urlparse, parse_qs
# --- Configuration ---
PORT = 8080
REDIRECT_URL = f"http://localhost:{PORT}"
SOUNDTRACK_URL = f"https://app.soundtrack.io/connect/generic?redirect={REDIRECT_URL}"
# --- Simple HTTP Server ---
class PairingCodeCallbackHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Parse the request URL to find the 'code' parameter
query_components = parse_qs(urlparse(self.path).query)
code = query_components.get("code", [None])[0]
print(f"Received code: {code}")
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
raise SystemExit
# --- Main execution ---
try:
with socketserver.TCPServer(("", PORT), PairingCodeCallbackHandler) as httpd:
print(f"Starting local server on port {PORT}...\n")
print("1. A browser window will open.")
print("2. Log in using your test account and select a zone to connect to.")
print("3. Soundtrack redirects back here and the code appears in the terminal.")
print("4. Copy the code for use with your SDK.")
webbrowser.open(SOUNDTRACK_URL)
httpd.serve_forever()
except OSError as e:
print(f"Could not start server on port {PORT}: {e}")The reference app implements this method in src/routes.c: GET /api/pair-redirect returns the connect URL, and GET /api/auth/callback receives the code and calls initiate_pair_with_code. It runs on localhost, so it works with the generic endpoint.
Important notes
- The
genericendpoint is for development and testing only, and only allows redirects to a localhost address. Once you are finished with testing, we will provide a unique slug for your project with specific allowed redirects. - Your application must handle the redirect and read parameters from the callback URL.
- Your application's UI must clearly explain the redirect to the user to avoid confusion.
Manual entry
- The user logs in to their Soundtrack account in the Soundtrack web app on any device.
- The user navigates to the sound zone they want to stream music from and generates a connect code by selecting
Connect. - The connect code is displayed on the page.
- Your application (e.g., the companion app or a screen on the player itself) must provide a place for the user to type in this code.
- Your application passes the code to the SDK to complete the connection.
Disconnecting from a sound zone
To disconnect the player from its sound zone, call unpair or unpair_sync. unpair is a deferred request: it only schedules the disconnect and returns before it has completed, so connecting immediately afterwards can fail with T_FAIL_ALREADY_PAIRED. Either poll get_auth_status until it returns SPLAYER_AUTH_STATUS_NOT_PAIRED before connecting again, or use unpair_sync.
unpair_sync combines unpair and get_auth_status and returns once the disconnect has completed. It returns 0 when the player is ready to be connected again, including when it was not connected to begin with, otherwise non-zero. It is safe to call repeatedly.
- If the call times out, the disconnect keeps running in the background. Calling
unpair_syncagain waits for it to complete. - If the SDK shuts down before the disconnect has completed, the player may still be connected after a restart. Check
get_auth_statusat startup and disconnect again if needed.