Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Created March 10, 2024 17:41
Show Gist options
  • Save benigumocom/89082b00a1e7a1c3ea74847df9de892b to your computer and use it in GitHub Desktop.
Save benigumocom/89082b00a1e7a1c3ea74847df9de892b to your computer and use it in GitHub Desktop.
import Foundation
import SwiftUI
import AVFoundation
struct SystemSoundWheelPicker: View {
@State private var sounds: [SystemSoundID] = []
@State private var playingID = -1
var body: some View {
Picker("", selection: $playingID) {
ForEach(sounds.sorted(), id: \.self) { soundID in
Text(String(soundID))
.tag(Int(soundID))
.monospaced()
.bold()
}
}
.onAppear {
for i in 1 ... 4000 {
let soundID = SystemSoundID(i)
let start = Date()
AudioServicesPlaySystemSoundWithCompletion(soundID) {
let elapsed = Date().timeIntervalSince(start)
Task { // main thread
if elapsed > 0.0025 {
sounds.append(soundID)
}
}
}
}
}
#if os(iOS)
.pickerStyle(.wheel)
#endif
.onChange(of: playingID) { _, new in
print(new)
AudioServicesPlaySystemSound(SystemSoundID(new))
}
}
}
#Preview {
SystemSoundWheelPicker()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment