Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created August 25, 2023 18:56
Show Gist options
  • Save christianselig/c15239e0daf73f2e94e16dda3a67c19e to your computer and use it in GitHub Desktop.
Save christianselig/c15239e0daf73f2e94e16dda3a67c19e to your computer and use it in GitHub Desktop.
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()
}
}
@temkich
Copy link

temkich commented Dec 19, 2023

How did you manage to add a custom property here? WidgetSound
can you show the implementation WidgetSound?

@amaider
Copy link

amaider commented Feb 26, 2024

How did you manage to add a custom property here? WidgetSound can you show the implementation WidgetSound?
https://developer.apple.com/documentation/appintents/transientappentity

@Vaberer
Copy link

Vaberer commented Jun 28, 2024

Hello @christianselig can you please give us example how WidgetSound object look like? Thank you  ♥️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment