Hi,
My app is a simple one, where someone taps on a button and a sound file is activated. But when that's done, the sound file doesnt activate.
Here's the code:
import SwiftUI
import AVFoundation
struct ContentView: View {
private var autoPlayer: AVAudioPlayer = AVAudioPlayer()
var body: some View {
GeometryReader { _ in
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
Circle()
.frame(width: 400, height: 400)
.foregroundColor(Color.white)
Circle()
.frame(width: 340, height: 500)
.foregroundColor(Color.red)
Button(action: {
print("Button Tapped!")
let path = Bundle.main.path(forResource: "siren", ofType: "mp3")!
do {
let playFile = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
playFile.play()
} catch {
print(error)
}
}) {
Text("Tap HERE for Alarm")
.font(.largeTitle)
.foregroundColor(Color.white)
.bold()
.italic()
}
}
}.navigationBarTitle(Text("Personal Alarm"))
}
func playSound()
{
print("Button Tapped!")
let path = Bundle.main.path(forResource: "siren.mp3", ofType: nil)!
let url = URL(fileURLWithPath: path)
do {
let soundFile = try AVAudioPlayer(contentsOf: url)
soundFile.play()
} catch {
print("Cannot Play Sound File!")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any ideas?
Dan Uff