I am trying to make an app where it will play an audio for a set amount of time from the user. I am currently using this code below in the viewDidLoad() function. At the top of the class I have created a
var player:AVAudioPlayer = AVAudioPlayer()
I get the audio to play no problem, but the problem starts when I try to pause or stop the audio. I have a pause button that I want to stop the audio but I can not seem to figure out how to stop the audio. Below I have attached some of the different lines of code to stop the audio but none of them work.
do {
let sound = Bundle.main.path(forResource: "audio1", ofType: ".mp3")
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
} catch{
print("Error in the audio file")
}
startAudio()
None of these will work when I try to stop the audio, I want to pause it because I will be having an option to either resume the timer with the audio, or cancel out and end the current session.
player.stop()
player.pause()
player.volume = 0.0
Any help with this would be greatly appreciated!