I build BetterMuslim, a React Native and Expo app for daily Muslim habits. It has a Fajr alarm that rings with a short Qur'an recitation from a reciter you pick. The core app is free with no ads, and there is an optional Supporter subscription. For its next perk I wanted something that costs nothing to host and locks no reciter away: wake up to the voice of someone you love. A parent, a spouse, a friend, or yourself, recorded on the phone.

It sounds like "add a record button". The work was in three places: the file format, where the file lives, and the recorder's lifecycle inside a bottom sheet. This is how it ended up, with the real code.

The constraint: the alarm only plays what it can find by name

I wrote about the alarm itself in an earlier post. The short version: on iOS the alarm sound is looked up by file name in Library/Sounds, and past about 30 seconds it falls back to the system tone. For reciters, a small native function downloads the recitation, decodes it, and writes a 16-bit PCM CAF file cut at 29 seconds.

My first idea was to reuse that converter for the recording. It doesn't work. The converter saves every download as .mp3 before decoding, and a phone can't record MP3. Fed an AAC recording under that name, Core Audio misread it; fed PCM, it refused it. I could have taught the native side to sniff the format, but that means a new store build.

The other way round was simpler: record straight into the format the converter writes. Then the recording needs no conversion at all, only a copy.

Recording directly into 16-bit PCM CAF

expo-audio lets you pass full iOS recording options, so the preset is the converter's output settings, written as JS:

const OWN_VOICE_RECORDING: RecordingOptions = { ...RecordingPresets.HIGH_QUALITY, extension: '.caf', numberOfChannels: 1, ios: { extension: '.caf', outputFormat: IOSOutputFormat.LINEARPCM, audioQuality: AudioQuality.MAX, linearPCMBitDepth: 16, linearPCMIsBigEndian: false, linearPCMIsFloat: false, }, }; 

Mono matters here. Uncompressed audio is big, and one channel keeps 29 seconds at about 2.5 MB.

The 29-second limit is enforced by the recorder, not by a warning. When the duration reaches it, recording stops on its own:

export const OWN_VOICE_SECONDS = 29; useEffect(() => { // isRecording: right after "Record again" the state can still hold the last take's length. if (phase === 'recording' && status.isRecording && status.durationMillis >= OWN_VOICE_SECONDS * 1000) stop(); }, [phase, status.isRecording, status.durationMillis]); 

That isRecording check is there because right after "Record again", the recorder state can still hold the previous take's length. Without it, a new take that follows a full 29-second one would stop the moment it starts.

Anything under a second is dropped and the screen goes back to "ready". An accidental tap shouldn't replace someone's mother's voice.

Two copies of the file, on purpose

The recorder writes to a temporary file. When the person taps "Use this voice", it's copied to Documents, which is the master copy the system never clears:

export function ownVoiceFile(): File { return new File(Paths.document, 'fajr-own-voice.caf'); } export function keepOwnVoice(recordingUri: string) { const target = ownVoiceFile(); if (target.exists) target.delete(); new File(recordingUri).copy(target); } 

When the alarm is saved, that file is copied again into Library/Sounds, where the alarm looks for it. expo-file-system has no constant for that folder, but it sits next to Caches inside Library, so the path is derived from the cache directory:

function soundsDirectory(): Directory { return new Directory(Paths.cache.uri.replace(/Caches\/?$/, 'Sounds/')); } function installOwnVoice(name: string) { const dir = soundsDirectory(); if (!dir.exists) dir.create({ intermediates: true, idempotent: true }); const target = new File(dir, name); if (target.exists) target.delete(); ownVoiceFile().copy(target); } 

Why not record straight into Library/Sounds? Because that folder is shared with the reciter sounds, and the native converter cleans it up: after writing a new reciter's file it deletes every other fajr-* file there. If someone tries a reciter and then switches back to their own voice, the copy in Sounds is gone, but the one in Documents isn't. So saving the alarm always reinstalls the own voice, even when the selected voice didn't change:

// The own voice may have been recorded again under the same id: always put it back in place. const soundChanged = !wasOn || next.sound !== saved.sound || next.reciterId !== saved.reciterId || isOwnVoice(next.reciterId); 

The same check covers the other case: a new recording under the same id, which a plain "did the id change" check would miss.

The result: the voice feature itself is JS only. expo-audio and the microphone permission were already in the app for an earlier feature, and the file work is expo-file-system, so the native code didn't change.

The recording is never uploaded. It stays on the phone, which also means there is nothing to store or moderate on a server.

Three bugs the simulator found

A crash on "Use this voice". The sheet stops the recorder on unmount, so a closed sheet never leaves the microphone open. My first version asked the recorder whether it was still recording. But useAudioRecorder releases its native object first, so the cleanup touched a released object and crashed. The fix is to keep my own flag and never read the recorder's state there:

useEffect( () => () => { // useAudioRecorder releases its native object first: read our own flag, never the recorder's. if (live.current) { try { recorder.stop().catch(() => undefined); } catch { // already released } } setAudioModeAsync({ allowsRecording: false }).catch(() => undefined); QuranAudioService.stop(); }, [recorder], ); 

Playback from the earpiece. On iOS, recording mode routes playback to the earpiece. Listening back to a take right after recording came out of the earpiece, quietly. Switching recording mode off as soon as recording stops fixes it:

await recorder.stop(); // Recording mode sends playback to the earpiece on iOS: switch it off before listening back. await setAudioModeAsync({ allowsRecording: false }).catch(() => undefined); 

The timer's digits swapped in Arabic. The app runs fully right to left in Arabic, and the counter 0:29 came out as ٢٩:٠. The bidi algorithm reorders the digit runs around the colon. Wrapping the time in left-to-right isolate characters keeps it in order:

const clock = (s: number) => `⁦${formatNumber(`0:${String(s).padStart(2, '0')}`)}⁩`; 

The voice's name had a smaller Arabic problem too: the row auto-shrinks long names to fit, and in Arabic it shrank this short one to a quarter of its size. The own voice's name doesn't auto-shrink now.

The paywall rule

This is the part I thought about most. Every reciter stays free. The own-voice row sits at the top of the voice sheet: Supporters get the recorder, others see a Supporter badge that opens the Supporter screen.

And a recording outlives the subscription. If someone records their father's voice and later cancels, the alarm keeps playing it. Only recording a new one needs the plan:

const pick = () => { stopPreview(); if (ownSaved) return onSelect(OWN_VOICE_ID); // a kept recording is always selectable if (canRecord) return setRecording(true); // Supporters record onClose(); router.push('/supporter?source=fajr_voice'); // everyone else sees what it is }; 

Taking a voice away from someone's alarm because a payment lapsed felt wrong, and the rule costs nothing to keep.

It's iOS only. On Android the alarm can't play a chosen sound yet, so the perk is only listed where the alarm can play it.

What I'd take from this

  • If a native step only accepts one format, check whether you can produce that format at the source. Recording straight into PCM CAF removed a conversion and a store build.
  • Keep the master copy of user-made media somewhere the system and your own cleanup code never touch, and install from it every time.
  • In a recorder's unmount cleanup, don't read the hook's native object. Track the state yourself.
  • Any time string in an RTL layout needs bidi isolation, or the digits move.

The Fajr alarm and the voice recorder are in BetterMuslim if you want to see them working: bettermuslim.net.