I have a counter that plays a "tick" sound everytime its tapped. I tried the app on my physical device and the sound plays even when the phone is in silent mode. How do I make it stop playing the sound when the silent mode is on?
import AVFoundation
struct View1 : View {
@State var player : AVAudioPlayer?
var body: some View {
Button(action: {
playSound()
todoItem.count += 1
}) {
Image("CounterButton")
.resizable()
.scaledToFit()
.frame(width: 70, height: 70)
.foregroundColor(Color.white)
}
}
}
func playSound() {
guard let url = Bundle.main.url(forResource: "tick", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
/* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}