Created
March 10, 2024 17:41
-
-
Save benigumocom/89082b00a1e7a1c3ea74847df9de892b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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