Watchkit
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
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