0
votes

I call the superPlay function to start audio playback a little while later I call the superPlay function again to stop the playback. This does not work though because player.isPlaying is false even knowing the sound is clearly playing in an infinite loop. I have no idea why please help!

   func superPlay(timeInterval: TimeInterval, soundName: String) {
        do {
            alarmSound = soundName
            //set up audio session
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.defaultToSpeaker, .duckOthers])
            try AVAudioSession.sharedInstance().setActive(true)

            let url = Bundle.main.url(forResource: alarmSound, withExtension: "mp3")

            player = try! AVAudioPlayer(contentsOf: url!)
            player.numberOfLoops = -1
            //Start AVAudioPlayer
            print(timeInterval)
            print(time)
            let playbackDelay = timeInterval  // must be ≥ 0

            if player.isPlaying {
                player.stop()
            } else {
                player.play(atTime: player.deviceCurrentTime + playbackDelay) //time is a TimeInterval after which the audio will start
            }
        }
        catch {
            print(error)
        }
    }

I have spent a couple more days debugging this now. What is happening is the original play is being assigned to a specific AudioPlayer ID for example the print is: "Optional(<AVAudioPlayer: 0x600002802280>)" When I call the function again to stop the play the AVAudioPlayer is assigned a different ID therefore it does not find that the old player is still playing and moves forward with playing a new sound on top of the old sound.

I am not sure how to store the AVAudioPlayer ID and then call the function so that it checks the store Player for if it is ".isPlaying"??

1
If the delay is happening, the player is not playing yet.matt
I ended up finding that the audioPlayer object was being saved in the file that was calling the function which initiated the audioPlayer to play. So I added the stop to that file and now it works.core

1 Answers

0
votes

This happen because you initialize it before stopping current audio. Try this way:-

    func superPlay(timeInterval: TimeInterval, soundName: String) {

        //        If audio is playing already then stop it.
        if let audioPlayer = player {
            if player.isplaying {
                player.stop()
            }
        }
        //        Initilize audio player object with new sound
        do {
            alarmSound = soundName
            //set up audio session
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.defaultToSpeaker, .duckOthers])
            try AVAudioSession.sharedInstance().setActive(true)

            let url = Bundle.main.url(forResource: alarmSound, withExtension: "mp3")

            player = try! AVAudioPlayer(contentsOf: url!)
            player.numberOfLoops = -1
            //Start AVAudioPlayer
            print(timeInterval)
            print(time)
            let playbackDelay = timeInterval  // must be ≥ 0

            player.play(atTime: player.deviceCurrentTime + playbackDelay) //time is a TimeInterval after which the audio will start
        }
        catch {
            print(error)
        }
    }