Skip to content

Instantly share code, notes, and snippets.

@erica

erica/ContentView.swift Secret

Created Jun 5, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.