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;
Each call to getAudioChunk holds one 2 MB piece and its base64 copy. The full recording only ever exists in the browser.

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 at 0:00 / 0:00; the console shows a media-src violation. Use a Blob and URL.createObjectURL, and revoke it.
  • A 6 MB synchronous heap cannot hold a 6 MB file and its base64 copy. Return one chunk per @AuraEnabled call and assemble the Blob in the browser.
  • RingCentral recording content honored Range with 206 Partial Content and a Content-Range total. bytes=0-0 returned 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 429 to the component as a retryable state with the same offset, not as an error.

Filed under

Disclosure: Doorvana's owner is also a co-owner of Nocona Consulting. This is a real engagement between two separate companies, but not an arm's-length one — and not an independent reference. More on the work page.

FAQ

Why won't an audio element play a data: URL in a Lightning Web Component?

Lightning's Content Security Policy does not allow data: URLs as a media source. The player renders and stays at 0:00 / 0:00, nothing errors in Apex or in the component, and the only evidence is a media-src violation in the browser console. Decode the base64 into a Blob and use URL.createObjectURL instead; blob: URLs are allowed. Revoke the URL when it is replaced or the component disconnects.

How do you pass a file larger than the Apex heap limit to a Lightning Web Component?

Send it in pieces. A synchronous Apex transaction has a 6 MB heap, and a file plus its base64 copy is about 2.3 times the file size, so a 6 MB file cannot pass through one call. If the source server honors HTTP Range requests, an @AuraEnabled method can fetch and return one chunk per call with the next offset, and the component loops, collects the decoded Uint8Array pieces and builds one Blob.

Does RingCentral's recording content endpoint support HTTP Range requests?

In this build, yes. A request with Range: bytes=0-1999999 returned 206 Partial Content with a Content-Range header giving the byte range and the total size. One oddity: Range: bytes=0-0 returned the whole file with a 206 status rather than a single byte, so probe with a realistic range.

How do you avoid hitting RingCentral's token rate limit from Apex?

Do not request a new access token in every transaction. RingCentral's token endpoint reported a limit of 5 requests per 60 seconds. Cache the access token, which is valid for an hour, in an org Platform Cache partition so scheduled jobs and user actions share it, and if a cached token comes back 401, drop it and request a new one once.

Working on something like this?

I can tell you what it takes.

If you run a home services business and this looks like a problem you have, book a call and we will talk through it.