Skip to content

Instantly share code, notes, and snippets.

@erica
Created June 5, 2020 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/7b9c43b26e759df651c767e126b312b7 to your computer and use it in GitHub Desktop.
Save erica/7b9c43b26e759df651c767e126b312b7 to your computer and use it in GitHub Desktop.
Watchkit
import SwiftUI
import AVFoundation
var player: AVAudioPlayer? = nil
struct ContentView: View {
func play(_ name: String){
guard
let url = Bundle.main.url(forResource: name, withExtension: "wav"),
let _ = try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: []),
let _ = try? AVAudioSession.sharedInstance().setActive(true),
let aPlayer = try? AVAudioPlayer(contentsOf: url)
else { return }
player = aPlayer
player?.play()
}
var body: some View {
VStack {
HStack {
Button(action: { self.play("honk") }) {
Image("honk").resizable().renderingMode(.original)
}.contentShape(RoundedRectangle(cornerRadius: 8))
Button(action: { self.play("win") }) {
Image("win").resizable().renderingMode(.original)
}.contentShape(RoundedRectangle(cornerRadius: 8))
}
HStack {
Button(action: { self.play("oof") }) {
Image("oof").resizable().renderingMode(.original)
}.contentShape(RoundedRectangle(cornerRadius: 8))
Button(action: { self.play("ff") }) {
Image("ff").resizable()
}.contentShape(RoundedRectangle(cornerRadius: 8))
}
}.aspectRatio(contentMode: .fit)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment