Hi, I have the following code. How can I reset the button to "play" when the file stopped playing? Thank you for your help.
import SwiftUI
import AVFoundation
struct ContentView: View {
@StateObject private var soundManager = SoundManager()
@State var song1 = false
@State var buttonImage: Image = (Image(systemName: "play.circle.fill"))
var body: some View {
VStack {
Button(action: {
soundManager.playSound("https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_100KB_MP3.mp3")
song1.toggle()
if song1 {
soundManager.audioPlayer?.play()
updateButtonImage(image: "pause.circle.fill")
} else {
soundManager.audioPlayer?.pause()
updateButtonImage(image: "play.circle.fill")
}
}) {
buttonImage
.resizable()
.frame(width: 50, height: 50)
}
}
}
private func updateButtonImage(image: String) {
buttonImage = Image(systemName: image)
}
}
class SoundManager : ObservableObject {
var audioPlayer: AVPlayer?
func playSound(_ sound: String){
if let url = URL(string: sound) {
self.audioPlayer = AVPlayer(url: url)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}