Created
August 25, 2023 18:56
-
-
Save christianselig/c15239e0daf73f2e94e16dda3a67c19e to your computer and use it in GitHub Desktop.
This file contains 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
struct SoundMakerIntent: AudioPlaybackIntent { | |
static var title: LocalizedStringResource = "Play a sound" | |
static var description: IntentDescription = IntentDescription("Plays a widget sound") | |
@Parameter(title: "Sound") | |
var sound: WidgetSound | |
init() {} | |
init(sound: WidgetSound) { | |
self.sound = sound | |
} | |
func perform() async throws -> some IntentResult { | |
SoundPlayer.shared.play(sound) | |
return .result() | |
} | |
} | |
class SoundPlayer: NSObject { | |
static let shared = SoundPlayer() | |
private var player: AVAudioPlayer? | |
func play(_ sound: WidgetSound) { | |
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: []) | |
try? AVAudioSession.sharedInstance().setActive(true) | |
let soundURL = Bundle.main.url(forResource: sound.rawValue, withExtension: "mp3")! | |
guard let player = try? AVAudioPlayer(contentsOf: soundURL) else { return } | |
self.player = player | |
player.play() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How did you manage to add a custom property here? WidgetSound
can you show the implementation WidgetSound?