边刷手机,边跟唱 Sing along while you scroll
LyricSync 是一个 iPhone App:用麦克风听正在放的歌,几秒内认出来,把正在唱的那一句放到灵动岛、锁屏和浮在所有 App 上面的悬浮窗里。这是我做它的过程。 LyricSync is an iPhone app that hears the song through the microphone, recognizes it in seconds, and puts the line being sung on the Dynamic Island, the Lock Screen and a floating window above every app. This is how I made it.
设计工程师,独立负责 · 和 Claude Code 一起 · 1 天
SwiftUI · ShazamKit · ActivityKit · AVKit
Design engineer, solo · with Claude Code · 1 day
SwiftUI · ShazamKit · ActivityKit · AVKit
系统不给,那就自己听The system won't tell, so it listens
在 iPhone 上,想一边刷手机一边看着歌词唱歌,并不简单。歌词在音乐 App 里,一切出去就看不到了。最先撞上的是平台限制:苹果不向第三方 App 开放系统级的歌词,也不开放「别的 App 正在放哪首歌」。能读取的只有私有接口,用了就上不了架;正规能读到的只有 Apple 自带的「音乐」App,以及开发模式下最多 5 个人能用的 Spotify。 On an iPhone, reading lyrics while you scroll through other apps isn't easy. The lyrics live inside the music app and disappear the moment you switch away. The first wall was the platform: Apple doesn't give third-party apps system-level lyrics, or tell them which song another app is playing. The only way in is a private API, which means no App Store; the sanctioned options cover just Apple's own Music app, plus Spotify in development mode, capped at 5 users.
所以 LyricSync 不去问系统,而是自己听:用麦克风录音,交给 ShazamKit 认歌,同时拿到这首歌放到了第几秒。后面所有的设计问题都从这里来。因为只能「听」,认歌得快,换歌得自己发现,进度也只能自己推算。 So LyricSync doesn't ask the system. It listens: it records from the microphone, hands the audio to ShazamKit, and gets back the song and how many seconds in it is. Every problem after that comes from this. Because the app can only listen, recognition has to be fast, song changes have to be noticed by the app itself, and the position has to be worked out locally.
- 系统的正在播放和歌词System now-playing and lyrics
- 麦克风Microphone
- Shazam + 网易云识曲Shazam + NetEase
- 歌名 + 第几秒Song + position
我怎么做的How I worked
我负责产品方向和每一个取舍:要什么功能、做成什么样、哪里不对。代码基本由 Claude Code 写。 I owned the product direction and every tradeoff: what to build, how it should look, what was wrong. Claude Code wrote nearly all of the code.
我在自己的 iPhone 上一遍遍试用,看真机日志,把问题和要求反馈回去,再验证改完的版本。下面三个决定,都是在这样的来回里定下来的。 I tested each build on my own iPhone, read the device logs, sent the problems and requirements back, and checked the fixes. The three decisions below came out of that loop.
3 秒内认出来Recognize within 3 seconds
歌词要跟得上,前提是认得够快。一首歌放了十几秒才认出来,前面几句就错过了。所以两个来源同时认,谁先出结果用谁:Shazam 流式比对,录音一边进来一边认;网易云听歌识曲中文歌更全,但只接受 3 秒或 6 秒的录音(4、5 秒一次都认不出),所以攒够 3 秒就先送出去,不等 6 秒。 Lyrics can only keep up if recognition is fast. If it takes fifteen seconds, the first lines are gone. So two sources run at once and the first answer wins: Shazam streams, matching while audio is still coming in; NetEase covers Chinese songs better but only accepts 3 or 6 second clips (4 and 5 never matched), so the app sends 3 seconds the moment it has them.
- 0.3–0.8s
- 网易云每次识别返回结果的时间。How long each NetEase request takes to answer.
- ≈2.6s
- 从开始听到第一次认出歌,用 3 秒录音。From starting to listen to the first match, with a 3 second clip.
- 3s
- 送去识别的最短录音,再短网易云就认不出。The shortest clip sent; any shorter and NetEase won't match.
数字来自真机调试日志。Measured from device logs.
// ShazamRecognizer.swift /// NetEase only matches 3 s or 6 s clips. 3 s hits about as /// often as 6 s, so send it the moment we have it. static let neteaseShortClip: TimeInterval = 3 static let neteaseAttemptsPerRound = 4 static let neteaseRetry: TimeInterval = 1.5
换歌要自动Song changes have to be automatic
手动切歌太繁琐了。人在 KTV 或者刷着手机,不会每首歌都回到 App 里点一下「重新识别」。难点是认错:真机上见过 Shazam 好几次把正在放的小众中文歌认成不相干的英文歌,要是一认成别的歌就换,歌词会乱跳。拖动下面的滑块,看一次换歌是怎么被确认的。 Switching songs by hand is too much work. Nobody at karaoke, or mid-scroll, goes back into the app to tap "re-sync" for every song. The hard part is false matches: on device, Shazam more than once matched an obscure Chinese song to an unrelated English track, and switching on every new match makes the lyrics jump. Drag the slider to watch a song change get confirmed.
正在跟唱《夜航星光》Following along to the current song
识别结果和当前这首一致,就只校准进度,歌词照常走。 The match agrees with the current song, so only the timing is corrected and the lyrics carry on.
规则:认成另一首歌时先不换,马上再认一次。另一个来源也这么说,就换;只有同一个来源在说,Shazam 要连续 3 次、网易云要连续 2 次。 The rule: when a different song comes back, don't switch yet, check again right away. If the other source agrees, switch. If only one source says so, Shazam needs 3 matches in a row and NetEase needs 2.
var pending = pendingSwitch.flatMap { $0.track.isSameSong(as: track) ? $0 : nil } ?? (track: track, sources: [], count: 0) pending.sources.insert(source) pending.count += 1 let needed = source == .netease ? neteaseOnlyConfirmations // 2 : shazamOnlyConfirmations // 3 let confirmed = pending.sources.count > 1 || pending.count >= needed if !confirmed { pendingSwitch = pending return Timing.confirmSwitch // check again in 1 s }
不要服务器,全在手机上算No server, all on the phone
我不想维护一台服务器。所以每件事都在手机上完成,请求从用户自己的网络直接发出。 I didn't want a server to maintain. So everything runs on the phone, and requests go out over the user's own connection.
声纹在设备上生成The signature is made on the device
ShazamKit 在手机上把几秒声音变成声纹,再去比对是哪首歌、放到第几秒。ShazamKit turns a few seconds of sound into a signature on the phone, then matches the song and the position.
指纹在手机上算The fingerprint is computed locally
网易云识曲要的音频指纹,用藏在 App 里的一个 WebView 跑 WebAssembly 算出来。只把指纹发出去,不上传录音。The audio fingerprint NetEase needs is computed by WebAssembly in a hidden WebView. Only the fingerprint is sent, never the recording.
歌词直接查Lyrics are fetched directly
从网易云、LRCLIB 和 YouTube Music 直接查,按歌名、歌手和时长打分,挑最像的一条。Straight from NetEase, LRCLIB and YouTube Music, scored by title, artist and duration; the closest match wins.
进度靠本地时钟A local clock keeps time
认出后,进度 = 认出时的秒数 + 过去了多久,不用一直请求接口。只在暂停、换歌或定时抽查时重新识别。After a match, position = matched offset + elapsed time, with no polling. It only re-recognizes on a pause, a song change or a scheduled check.
func position(at now: Date = .now) -> TimeInterval {
offset + rate * now.timeIntervalSince(date)
}
横过来,就是 KTVTurn it sideways for karaoke
点右上角的全屏按钮,手机横过来就是古早 KTV 字幕:蓝字白边,唱到哪变红到哪,两行一左一右轮流唱,进歌词前有圆点倒数。 Tap full screen and turn the phone sideways for retro karaoke subtitles: blue text with a white outline that turns red as it's sung, two lines taking turns left and right, with a dot countdown before each verse.
歌词出现在哪里Where the lyrics show up
- 灵动岛Dynamic Island刷别的 App 时,正在唱的一句就在屏幕顶上。While you're in another app, the current line sits at the top of the screen.
- 悬浮窗Floating window画中画歌词浮在所有 App 上面,可以拖动、缩放。Picture-in-picture lyrics float above every app; drag and resize them.
- 五套主题Five themes纸白、樱花、薄荷、海盐、霓虹,各有深浅两版。Paper, sakura, mint, ocean and neon, each in light and dark.
- 字号和字体Size and type字号、字体、对齐和高亮方式都能调。Size, typeface, alignment and highlight style are all adjustable.