The recording was right there, and the player sat at 0:00
Streaming RingCentral call recordings into a Lightning Web Component without storing them in Salesforce. Why a data: URL player never loads (Content Security Policy media-src), why a 6 MB recording cannot pass through one Apex transaction, how HTTP Range requests and a Blob in the browser get around the heap limit, and why every chunk requesting its own access token would have hit a 429 within a minute.
The call recording was right there on the record, and it would not play. The player showed
0:00 / 0:00 and sat there. No error in the component, no error in Apex, nothing in the debug
log.
Once that was fixed, the longest calls, the ones a salesperson most wants to hear again, showed a polite refusal instead: too long to play here.
Both problems came from limits the platform does not announce until you hit them.
Where the audio comes from
At Doorvana every call to the office is pulled from RingCentral into a custom
Phone_Activity__c record by a server-side sync.
Recordings and voicemails were meant to play on that record. Nothing is stored in Salesforce:
the audio stays in RingCentral and is fetched when someone presses play, so access to a
recording follows access to the record.
RingCentral recording URLs need the API bearer token, so there is no plain link to hand a user.
Apex has to fetch the audio through the RingCentral Named Credential and give it to a
Lightning Web Component. The first version returned it as base64 and the component did the
obvious thing:
<audio src="data:audio/mpeg;base64,…">
data: is not a media source in Lightning
The only evidence was one line in the browser console: a Content Security Policy
violation, media-src blocked data. Lightning’s CSP does not allow data: URLs for media.
The element renders, the controls draw, and it never loads.
blob: is allowed. The component now decodes the base64 into bytes, builds a Blob, and hands
<audio> an object URL, revoking it whenever it is replaced or the component goes away:
force-app/main/default/lwc/phoneActivityAudio/phoneActivityAudio.js
const { parts, contentType } = await this.download(kind);
this.audioSrc = URL.createObjectURL(new Blob(parts, { type: contentType }));
// ...
disconnectedCallback() {
this.releaseAudio();
}
releaseAudio() {
if (this.audioSrc) {
URL.revokeObjectURL(this.audioSrc);
}
this.audioSrc = undefined;
} It played, after a hard reload cleared the cached component. That last part matters: until the browser drops the old bundle, the fixed component is not the one running.
A 6 MB recording and a 6 MB heap
A 26-minute sales call came back with “This recording is too long to play here (6 MB). Open it in RingCentral.” That message was mine. The first version capped playback at 2.5 MB because of arithmetic it could not escape: a synchronous Apex transaction has a 6 MB heap, and the response body plus its base64 copy (about 1.33 times larger) means a 6 MB recording needs well over 6 MB of heap before anything else is counted.
The way out depended on one question about RingCentral: would its recording content endpoint
honor an HTTP Range header? A probe said yes:
Range: bytes=0-1999999
→ 206 Partial Content
Content-Range: bytes 0-1999999/6211629
So the @AuraEnabled method stopped returning a file and started returning a piece of one,
with where to continue:
force-app/main/default/classes/PhoneActivityAudioController.cls
/** Bytes per chunk: the chunk plus its base64 copy stays near 4.7 MB of the 6 MB heap. */
@TestVisible
private static final Integer CHUNK_BYTES = 2000000;
@AuraEnabled
public static AudioChunk getAudioChunk(Id phoneActivityId, String kind, Long offset) {
// ... read the URI WITH USER_MODE, so a user who cannot see the record cannot play it
Long start = offset == null || offset < 0 ? 0 : offset;
Long last = start + CHUNK_BYTES - 1;
response = new RingCentralClient().getRaw(uri, 'bytes=' + start + '-' + last);
// ...
chunk.base64 = EncodingUtil.base64Encode(body);
chunk.totalBytes = totalFromContentRange(response.getHeader('Content-Range'));
if (response.getStatusCode() == 206 && chunk.totalBytes != null && start + body.size() < chunk.totalBytes) {
chunk.nextOffset = start + body.size();
}
return chunk;
} The component calls it in a loop, decodes each piece into a Uint8Array, shows progress from
totalBytes, and builds one Blob at the end:
force-app/main/default/lwc/phoneActivityAudio/phoneActivityAudio.js
const chunk = await getAudioChunk({ phoneActivityId: this.recordId, kind, offset });
// ...
parts.push(Uint8Array.from(atob(chunk.base64), (character) => character.charCodeAt(0)));
contentType = chunk.contentType || contentType;
offset = chunk.nextOffset; Peak heap per call came out around 2.7 MB. The 6.2 MB recording arrives in four calls, and the user sees “Loading audio… 40%” for a few seconds instead of a refusal.
One oddity from the probe is worth knowing if you test this yourself: Range: bytes=0-0
returned the whole 6.2 MB file with a 206 status, not one byte. Probe with a realistic
range. The controller also refuses to pass on a 200 that is larger than one chunk, in case a
server ignores the header and sends everything.
Four chunks, four tokens, one rate limit
The same probe read RingCentral’s rate-limit headers, and they were the near miss. The token
endpoint reported X-Rate-Limit-Group: auth and X-Rate-Limit-Limit: 5, per 60 seconds.
Content downloads sit in the heavy group, 10 per 60 seconds. And every Apex transaction was
requesting its own access token. Four chunks meant four token calls. Two people pressing play
in the same minute, plus the five-minute sync, would have hit 429.
The access token is valid for an hour, so it now lives in an org Platform Cache partition, shared by the sync and by playback:
force-app/main/default/classes/RingCentralClient.cls
public void authenticate() {
String cached = readCachedToken();
if (String.isNotBlank(cached)) {
accessToken = cached;
tokenFromCache = true;
return;
}
requestNewToken();
}
/** A cached token RingCentral rejected (revoked, or the JWT changed): drop it and get a new one. */
private Boolean retryWithNewToken(Integer status) {
if (status != 401 || !tokenFromCache) {
return false;
}
clearCachedToken();
requestNewToken();
return true;
} The token goes into Cache.Org.getPartition('local.RingCentral') with a time-to-live a little
under RingCentral’s expiry, so it never dies mid-transaction. If the partition is missing or
full, every transaction simply authenticates, as before. The cache is turned off in tests, so
a test run can never read a live org token.
A 429 on a chunk no longer surfaces as an error. The controller returns it as “rate limited”
with the same offset, and the component waits and asks again:
force-app/main/default/classes/PhoneActivityAudioController.cls
} catch (RingCentralClient.RingCentralException failure) {
if (failure.statusCode == 429) {
chunk.rateLimited = true;
chunk.nextOffset = start;
return chunk;
}
throw error('RingCentral did not return the audio: ' + failure.getMessage());
} Practical summary
- Lightning’s CSP blocks
data:as a media source. The player renders and stays at0:00 / 0:00; the console shows amedia-srcviolation. Use aBlobandURL.createObjectURL, and revoke it. - A 6 MB synchronous heap cannot hold a 6 MB file and its base64 copy. Return one chunk per
@AuraEnabledcall and assemble theBlobin the browser. - RingCentral recording content honored
Rangewith206 Partial Contentand aContent-Rangetotal.bytes=0-0returned the whole file. - RingCentral’s token endpoint reported 5 requests per 60 seconds. Keep the one-hour access token in an org Platform Cache partition shared by every caller.
- Return a
429to the component as a retryable state with the same offset, not as an error.