Skip to content

Instantly share code, notes, and snippets.

@hguandl
Created August 28, 2022 04:15
Show Gist options
  • Save hguandl/e279871653ad28bd03bc7f5dc11727bf to your computer and use it in GitHub Desktop.
Save hguandl/e279871653ad28bd03bc7f5dc11727bf to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Shared
//
// Created by hguandl on 28/8/2022.
//
import AVFoundation
import SwiftUI
struct ContentView: View {
@State private var player: AVAudioPlayer?
var body: some View {
VStack {
Button("Play") {
playSound()
}
.padding()
}
.frame(minWidth: 300, minHeight: 200)
}
private func playSound() {
guard let url = Bundle.main.url(forResource: "music", withExtension: "flac") else { return }
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch {
print(error.localizedDescription)
}
}
}
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