Docs
Media

Voice Recorder

Hold to record a voice message: the glass mic swells into a recording capsule; slide to cancel or lock, let go to send.

By · Updated

Voice Recorder: SwiftUI Hold to Record Voice Message for iOS, live preview in light mode

Voice Recorder, a SwiftUI Hold to Record Voice Message for iOS 17+, in its live preview. Open Code for the Swift source.

$npx swiftpieces add VoiceRecorder
Free · MIT + Commons ClauseiOS 17.0+Liquid Glassvoice-messageaudiorecordmicrophonechatcomposerhold-to-recordmedia
Type
VoiceRecorder
Files
VoiceRecorder.swift
Depends on
Nothing (Apple frameworks only)
Version
1.0.0
  • Info.plist NSMicrophoneUsageDescription: Records voice messages you send.

The hold-to-record voice message button for a chat, notes, journaling or support composer. Your field sits at rest with a glass mic beside it. Press and hold the mic: after a short beat (so taps and scrolls never record) it swells red and a glass capsule buds out of it over your field, with the elapsed time and live speech bars scrolling toward the leading edge, while a lock lane buds up above it. Then one of three things: - Let go to send. The capsule melts back into the mic and onSend gets a Recording: an .m4a in the temporary directory, its duration, and loudness samples that drop straight into WaveformScrub(samples:). - Slide toward the leading edge to discard. A glass bin buds out at the threshold; let go there (or keep going) and the capsule gathers into it, it tips closed and melts away, the file is deleted and onCancel runs. - Slide up the lane to lock. The recorder goes hands-free: the mic turns into a red send button and a trash bubble buds out of the capsule. Tap send to finish or trash to discard. Holds shorter than minimumDuration and plain taps are discarded with a small hint that teaches the gesture. The first press asks for the microphone (add NSMicrophoneUsageDescription to your Info.plist); when access is off the hint offers Settings. Over the last ten seconds before maximumDuration the time counts down on red, and at the maximum the take is sent. A call, Siri, an alarm, a lost route, the app going to the background or the view going away discards a held take and stops a locked one, keeping it so Send and Delete still work. swift @State private var draft = "" VoiceRecorder { recording in chat.sendVoice(recording.url, duration: recording.duration, waveform: recording.samples) } content: { TextField("Message", text: $draft) } Pass a Session you own to drive it from code (start(), lock(), send(), cancel()) or to read its phase and level; Session(source: .simulated) records a speech-like tone instead of the microphone, for previews and the simulator.

Notes

  • One liquid glass control beside your field. At rest a glass mic rests apart from your content at the trailing end. Recording, the mic swells red and a glass capsule buds out of it over your content, joined to it by a short neck, while a narrow lock lane buds up out of it above. Hands-free, a trash bubble buds out of the capsule's leading end and the mic becomes a red send button. Red appears only on the recording mic, the send button, the recording dot and the last-ten-seconds countdown. On iOS 26 this is Liquid Glass; before that, a frosted material; under Reduce Transparency, solid fills.
  • Put it where your composer is, usually in .safeAreaInset(edge: .bottom) under a chat ScrollView, and pass your field as content. Your content is shown as it is (style it yourself) and fills the leading space; it fades out under the capsule while recording and comes back as the capsule melts home. Without content the mic rests alone at the trailing end and the capsule spans the whole width.
  • Add NSMicrophoneUsageDescription to your Info.plist (the piece's metadata carries "Records voice messages you send."). The first press asks for the microphone and records nothing; when access is granted the hint "Hold to record, release to send" buds out, and when it is off the hint reads "Microphone access is off" with Settings, which opens your app's settings. Without the usage description it never asks (asking would end the app): it shows the access hint and, in debug builds, prints a reminder.
  • Press and hold: after 0.18 seconds with the finger still, recording starts with a medium tap. A quicker tap, or a hold shorter than minimumDuration (0.6 seconds), is discarded and teaches the gesture with the hint. A finger that moves first is a scroll: the press gives way and the enclosing scroll view or sheet moves as usual. Once recording, nothing steals the drag.
  • Let go to send. The capsule melts back into the mic with a light tap and onSend gets a Recording: url, an AAC .m4a (24 kHz, mono) in the temporary directory that is now yours to upload, move or delete; duration in seconds; and samples, loudness from 0 to 1 at about 20 a second (at most 160, the peak of each stretch), ready for WaveformScrub(samples:duration:…).
  • Slide toward the leading edge to discard. The mic follows the finger, the capsule shortens with it, "Slide to cancel" is eaten from its far end and fades, and the lock lane melts away. At the threshold (110 points, less on narrow screens) a glass bin buds out at the leading end with a selection tick; let go there, or keep going well past it, and the capsule gathers into the bin, which tips closed and melts away with a warning tap. The file is deleted and onCancel runs. Back short of the threshold, the bin melts home and everything returns.
  • Slide up to lock. The lane stretches with the finger; 56 points up the lock shuts with a rigid tick, the lane melts into the mic, the trash bubble buds out of the capsule and the mic turns into the red send button. Tap send to finish, tap trash to discard. A hold on the send button sends when it lifts.
  • The capsule shows a red dot that breathes once a second, the elapsed time (0:07, 1:02:07 when takes can run an hour; monospaced, so nothing shifts) and live bars, one every tenth of a second, scrolling toward the leading edge from beside the mic. Before there is sound the row is quiet dots. Over the last ten seconds before maximumDuration (5 minutes) the time counts down on a red block with ink digits, and at the maximum the take is sent, held or locked, with a success tap.
  • Interruptions: a call, Siri, an alarm, a lost audio route, reset media services, the app leaving the foreground, the control being disabled or the view going away discard a held take (the hint reads "Recording stopped" and onCancel runs) and stop a hands-free one, keeping it: the dot greys, the time and bars freeze, and Send and Delete still work. The audio session is set to play and record for each take and put back to the app's own category afterwards with .notifyOthersOnDeactivation, so other apps' audio resumes.
  • Recording runs off the main actor. Takes are numbered, so a release during a slow microphone start never leaves a recorder running, and every discarded take's file is deleted. A take is too short by how long it was held, so a slow start never turns a real hold into a hint.
  • VoiceRecorder.Session is the recorder's state and audio. Keep one in @State and pass session: to drive it from code: start() (hands-free at once), press() and release() (push-to-talk from your own control), lock(), send() and cancel(). Read phase (.idle, .holding, .locked, .stopped), level (0 to 1, live) and elapsed, for example to dim the conversation while recording. One session drives one recorder.
  • Session(source: .simulated) records no microphone and needs no permission: a deterministic speech-like level (phrases with short pauses) drives the bars, and the sent file is a soft tone shaped by it, so the URL plays and its waveform matches. Use it in SwiftUI previews, on the simulator and for screenshots.
  • Haptics: medium when recording starts, a selection tick at the discard threshold (both ways), rigid when it locks, a warning on discard, light on send, soft with a hint, success when the maximum sends it. Never per frame.
  • VoiceOver can't hold and drag, so the mic is a button, "Record voice message": a double tap starts a hands-free take at once, "Recording" is announced, and Send and Delete are buttons. The capsule is one element, "Recording", whose value is the elapsed time ("12 seconds", or "9 seconds left" in the countdown); it stays quiet while recording. The mic's actions are Record, and while recording Send and Delete. Escape (the two-finger scrub) deletes, and a two-finger double tap starts or sends. Hints are announced.
  • Hardware keyboard: while hands-free, Return sends and Escape deletes.
  • Reduce Motion: the mic doesn't swell, the chevrons don't drift, the capsule, lane, bin and hints open and close in place instead of budding, the bin doesn't tip, and digit changes fade. The bars still move: they are information. Pressed buttons dim instead of sinking.
  • Dynamic Type: the capsule, mic and glyphs grow with the text, part way and up to 1.4 times, so the bars keep their room; the time stops growing at the largest standard size. At accessibility sizes "Slide to cancel" drops to its chevron, and hints wrap onto more lines. All text is one weight, semibold.
  • Right to left mirrors everything: the mic sits on the left, discards slide to the right (the leading edge), the bars scroll right and the chevron points right. Hints wrap in the reading direction.
  • Style sets the red accent and the ink on it, the label and secondary label colours, the bars' colour, width and spacing, and the control's height (52 points, at least 44). Messages replaces any of the copy, such as slideToCancel: "Slide to delete" or holdToRecord; every default goes through your String Catalog.
  • Wire it up: in onSend, upload recording.url (or move it into your app's storage first), keep duration and samples with the message, and show it with WaveformScrub(samples: message.samples, duration: message.duration, …). Delete the file once it is uploaded.

Usage

VoiceRecorderExample()

Parameters

ParameterDescription
sessionThe recorder's state and audio. Pass your own to drive it from code or read its phase and level, or a Session(source: .simulated) for previews and the simulator. nil, the default, keeps one inside the view that records from the microphone.
maximumDurationThe longest take, in seconds. Over the last ten seconds the time counts down on red, and at the maximum the take is sent, held or locked. Defaults to 5 minutes.
minimumDurationThe shortest take that is sent, in seconds. Anything shorter, and any tap on the mic, is discarded with a hint that teaches the gesture. Defaults to 0.6 seconds.
messagesThe piece's copy: the hints, "Slide to cancel" and the VoiceOver labels. Defaults are localizable through your String Catalog; replace any line.
styleColours, the control's height and the bars. Defaults to the SwiftPieces house palette, adapting to light and dark, with red for the recording mic, the send button and the countdown.
onSendCalled with each finished take. The file is yours: upload it, move it out of the temporary directory, or delete it once sent.
onCancelCalled when a take is discarded: slid away, deleted, or stopped by an interruption while held. The file is already deleted. Not called for takes that are too short.
contentYour composer, usually a text field. It fills the leading space at rest and hides under the recording capsule while recording.

Install

Pick one. Run it from the folder that contains your .xcodeproj and the files land inside your app.

Terminal

$npx swiftpieces add VoiceRecorder

Or ask your coding agent · Claude Code, Cursor or Xcode, once the MCP server is connected

>Add the SwiftPieces "Voice Recorder" piece to my app

Or copy the source above into your app.

New to SwiftUI?If you know React, CSS or Tailwind, SwiftUI is easier to pick up than it looks.Try real app patterns first

Building a whole app? See SwiftPieces Pro →

All media →

Build it yourself: SwiftUI animations guide

On this page

SwiftPiecesPro

Build the whole app with one library.

Production-ready screens, whole flows, complete app templates, a Build Kit your agent follows and Pro remixing.

Explore Pro