Created
August 28, 2022 04:15
-
-
Save hguandl/e279871653ad28bd03bc7f5dc11727bf to your computer and use it in GitHub Desktop.
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
// | |
// 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