Upload and play audio and subtitle tracks
Table of contents
Part 1: Upload
- What you need before starting
- Method 1: Include subtitle tracks at upload time
- Method 2: Add tracks to an existing video
- Wait for tracks to be ready (webhooks)
- Update or remove tracks
- Quick reference (upload operations)
Part 2: Playback
- Web (FastPix Player)
- Android (FastPix Android Player SDK)
- iOS (FastPix iOS Player SDK)
- How track detection works (all platforms)
- Playback quick reference
Reference
Part 1: Upload a video with multiple audio and subtitle tracks
FastPix lets you attach multiple subtitle files and audio files to a single video. Subtitle tracks can be included at the time of upload or added afterward. Audio tracks can only be added after the video has been created. This section covers the full workflow.
What you need before starting
Before you begin, make sure you have the following ready:
- FastPix API credentials: a Token ID and Secret Key from your FastPix dashboard (Settings → API Keys). Combine them as
TokenID:SecretKeyand base64-encode the result for Basic Auth. - Your video file: either a public URL to the video (for URL-based upload) or the file itself (for direct upload).
- Your subtitle and audio files hosted publicly — FastPix fetches these by URL. Each file must be accessible without authentication. A CDN, S3 public bucket, or any public hosting works.
Supported formats:
If your file URL requires authentication (login, token, etc.), FastPix cannot fetch it and the track will fail. For S3, use a pre-signed URL with a generous expiry window.
Method 1: Include subtitle tracks at upload time
If your subtitle files are ready when you upload the video, you can attach them in the same API call by listing them in the inputs array alongside the video. Audio tracks cannot be included at upload time and must be added separately after the video is created (see Method 2).
Endpoint: POST https://api.fastpix.com/v1/on-demand
What’s happening here: You’re creating a single media asset that contains one video and three subtitle tracks (English, Spanish, Hindi). FastPix processes all of them together and attaches the subtitle tracks to the video automatically.
Input parameters for each subtitle track:
Response:
NOTE:
Save each track’s
idfrom the response. You’ll need thesetrackIdvalues if you want to update or remove individual tracks later.
This also works with the Direct Upload endpoint. Include your subtitle tracks inside the pushMediaSettings object. See the direct upload docs for the exact payload structure.
Method 2: Add tracks to an existing video
Once a video is uploaded, you can add both subtitle and audio tracks at any time using the tracks endpoint. This is also the only way to add audio tracks, since they cannot be included at upload time. Call the endpoint once per track.
Endpoint: POST https://api.fastpix.com/v1/on-demand/{mediaId}/tracks
Add a subtitle track:
Add an audio track:
There is no limit on the number of tracks per video. Call this endpoint once for each language or audio variant you want to add.
Response (audio track example):
SAVE THIS
Save
data.idas thetrackId. Audio tracks are registered but not immediately ready. Wait for thevideo.media.track.readywebhook before serving to viewers.
Wait for tracks to be ready (webhooks)
FastPix processes tracks asynchronously. After your API call returns, the track is queued for processing. You should listen for webhook events to know when tracks are ready for playback.
Key webhook events:
Production pattern:
- Call the add-track API and store the returned
trackIdin your database with status"processing". - Listen for
video.media.track.readyon your webhook endpoint. - When the webhook fires, match the
trackIdand update the status to"ready". - Only then expose the track to viewers in your player or UI.
Register your webhook endpoint in the FastPix dashboard under Settings → Webhooks. FastPix sends a POST request to your endpoint for each event.
Update or remove tracks
Update a track (fix a typo, swap the file, correct the language tag):
Delete a track:
NOTE:
Track deletion is permanent and cannot be undone. Double-check you have the correct
trackIdbefore sending the request.
Quick reference (upload operations)
Part 2: Playback videos with multiple audio and subtitle tracks
Once your tracks are processed and ready, the FastPix Player handles track switching automatically. This section covers how playback works on web, Android, and iOS.
The core idea is the same across all platforms: the FastPix Player reads the HLS manifest, detects all attached subtitle and audio tracks, and presents them as selectable options in the player UI. You don’t need to write custom track-switching logic.
Web (FastPix Player)
The FastPix web player is a custom HTML element (<fastpix-player>) that handles everything out of the box.
Install the player
CDN:
NPM:
See the installation guide for all options.
Embed the player
All subtitle and audio tracks attached to that playbackId appear automatically in the player UI:
- A CC button appears in the toolbar when subtitle tracks are detected. Clicking it shows a menu listing all available languages, plus an “Off” option.
- An audio icon appears when multiple audio tracks are detected. Viewers can switch between language dubs or audio variants.
If you add tracks after the video is already being played:
When tracks are added dynamically to a video that viewers are currently watching, enable cache busting to ensure the player fetches the updated HLS manifest:
This appends a unique query parameter to the manifest URL, forcing the browser and CDN to serve the latest version instead of a cached copy.
Secure playback (private videos)
For videos with a private access policy, pass a signed JWT token:
Track switching works identically for private and public videos. The token authenticates access to the stream; once the player loads the manifest, all tracks are available.
Complete working example (custom track switcher UI)
The following example shows a fully functional page that goes beyond the default player UI. It wires up custom audio and subtitle track buttons, renders a styled subtitle overlay driven entirely by your own code, polls for subtitle tracks after the manifest is parsed, keeps buttons in sync as tracks change, and supports forward/backward seek controls. It’s a starting point you can drop into any HTML page and extend.
Key points about the example above:
hide-native-subtitlessuppresses the player’s built-in subtitle layer so thecustom-subtitleoverlay is the only thing rendering subtitle text. Without this attribute, cue text would appear twice.default-audio-track/default-subtitle-trackaccept a track label (case-insensitive) and set the initially active track when the player loads. If the label doesn’t match any track in the manifest, the player falls back to the manifest default.fastpixtracksreadyis the correct entry point for all track work. Never callgetAudioTracks()orgetSubtitleTracks()before this event fires.- Subtitle polling after
fastpixtracksreadyis intentional — subtitletextTracksregister slightly later than audio tracks. PollinggetSubtitleTracks()for a short window catches them reliably. setAudioTrack()andsetSubtitleTrack()take a label string, not a numericid. Theidfield in theTrackInfoobject is an internal index and must not be used for switching or persistence.- Off button clearing happens synchronously in
onclick.fastpixsubtitlecuemay not fire again for several seconds after subtitles are turned off, so relying on the cue handler to clear the overlay causes a visible delay.
For a step-by-step breakdown of each part of this implementation — including how to persist user language preferences across sessions — see Build a custom track switcher UI.
Full API reference
For the complete reference — all methods, properties, events, attributes, and additional usage examples — see the Audio & Subtitle Tracks API reference in the FastPix web player repository.
React integration
To see how this track switcher and custom subtitle overlay are integrated in a React app (for example, a vertical shorts feed with a track menu and subtitle pill), see FastPix React Shorts Demo. That repo uses the same APIs (getAudioTracks, setAudioTrack, getSubtitleTracks, setSubtitleTrack, fastpixtracksready, fastpixsubtitlecue, and others), mounts the player with document.createElement('fastpix-player') in useEffect, and shows React patterns for refs, cleanup, and feed-level state. Clone it, run npm install and npm run dev, then replace the feed playback IDs with your own multi-track assets.
For a full deep dive on how the player detects and renders subtitle and audio tracks, see Manage audio and subtitles.
Android (FastPix Android Player SDK)
The FastPix Android Player SDK is built on Media3/ExoPlayer. It automatically detects subtitle and audio tracks from the HLS manifest and supports on-the-fly switching with no additional track-switching code required.
1. Add the dependency
In your app-level build.gradle (or build.gradle.kts for Kotlin DSL):
In your settings.gradle (or settings.gradle.kts), add the GitHub Maven repository. You’ll need a GitHub Personal Access Token (PAT) to access the private Maven package.
Note: Load credentials from
local.propertiesto avoid hardcoding secrets in your source files.
Click Sync Now after adding the repository.
2. Add the required imports
3. Set up playback
4. Listen for and switch audio tracks
The SDK discovers audio tracks from the HLS manifest and fires callbacks when they’re available:
To switch to a specific audio track:
NOTE:
If a seek is in progress, the SDK defers the track switch until the seek completes.
Example: Build a simple language menu
To set a preferred default audio track that applies automatically when tracks become available:
5. Listen for and switch subtitle tracks
To switch to a specific subtitle track or disable subtitles:
To set a preferred default subtitle track:
Note: Use BCP-47 or ISO language names such as
"English","Spanish","Hindi", or"French". Default track preferences apply automatically when tracks become available and never override a manual selection. If the preferred language isn’t present in the stream, the player retains its current selection.
Secure playback
For videos with a private access policy, pass a signed JWT token via playbackToken:
Track switching works identically for private and public videos.
For the full Android SDK reference, see FastPix player for Android.
iOS (FastPix iOS Player SDK)
The FastPix iOS Player SDK wraps AVPlayer and AVPlayerViewController. It auto-detects subtitle and audio tracks from the HLS manifest and supports dynamic audio track switching.
1. Install via Swift Package Manager:
In Xcode: File → Add Packages → enter the repository URL: https://github.com/FastPix/iOS-player
2. Import and set up playback:
3. Audio and subtitle track switching:
The player dynamically detects all available audio tracks from the HLS manifest. Users can switch audio tracks through the player’s built-in interface without restarting the stream. Subtitle tracks detected from the manifest are displayed automatically during playback.
For secure playback:
The iOS SDK also supports tvOS with the same API surface. See the tvOS setup guide for setup details.
Full SDK reference: FastPix player for iOS
How track detection works (all platforms)
Regardless of platform, the FastPix Player follows the same flow:
- Manifest parsing: When you provide a
playbackId, the player constructs the HLS stream URL and fetches the manifest. The manifest contains metadata entries for each subtitle and audio track, including language, name, and URI. - UI activation: If subtitle tracks are found, the subtitle/CC button is activated in the player interface. If multiple audio tracks are found, the audio selection control is activated.
- Default selection: The player selects the first subtitle track listed in the manifest and displays it by default. The default audio track is typically the original audio from the uploaded video.
- User switching: Viewers can switch languages or turn subtitles off through the player’s built-in menu. Audio track switching happens without restarting the stream.
Playback quick reference
FAQ
Can I upload a video with subtitle tracks in a single API call?
Yes. Include subtitle files in the inputs array alongside your video when calling POST /v1/on-demand. Each subtitle item needs type: "subtitle", a public url, and a languageCode. Audio tracks cannot be included at upload time and must be added separately via the tracks endpoint after the video is created.
Is there a limit on the number of tracks per video?
There is no stated limit. You can add as many subtitle and audio tracks as you need.
What if I add tracks to a video that viewers are currently watching?
On web, enable enable-cache-busting on the player to force a manifest refresh. On mobile, the next time the player loads the stream, it will pick up the new tracks.
How do I know my tracks are ready for playback?
Listen for the video.media.track.ready webhook. For audio tracks, there’s also a video.media.track.created event that fires earlier (registered but still processing). Don’t serve the track until you receive ready.
What language code format should I use?
BCP 47. Simple codes like en, es, fr, de, ja, hi work for most cases. For regional variants, use en-US, pt-BR, zh-TW, etc.
Next steps
- Add subtitles to a video — Full API reference for subtitle track operations.
- Add audio to a video — Full API reference for audio track operations.
- Manage audio and subtitles — Deep dive on how the web player handles track switching.
- FastPix player for Android — Full Android SDK integration guide.
- FastPix player for iOS — Full iOS SDK integration guide.
- Webhook event reference — Register your endpoint for real-time track processing events.
- Setup video using JWTs — Protect private media with signed tokens.