My loop makes a click noise each time it starts

The loop plays smoothly in audacity but when I run it in the device or simulator it clicks each loop at different intensities.

I config the session at App level:

        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(.playback, mode: .default, options: [.mixWithOthers])
            try audioSession.setActive(true)
        } catch {
            print("Setting category session for AVAudioSession Failed")
        }

And then I made my method on my class:

func playSound(soundId: Int) {
    let sound = ModelData.shared.sounds[soundId]
    
    if let bundle = Bundle.main.path(forResource: sound.filename, ofType: "flac") {
               let backgroundMusic = NSURL(fileURLWithPath: bundle)
               do {
                   audioPlayer = try AVAudioPlayer(contentsOf:backgroundMusic as URL)
                   audioPlayer?.prepareToPlay()
                   audioPlayer?.numberOfLoops = -1 // for infinite times
                   audioPlayer?.play()
                   isPlayingSounds = true
               } catch {
                   print(error)
               }
    }
}

Does anyone have any clue? Thanks!

PS: If I use AVQueuePlayer and repeat the item the click noise disappear (but its no use, because I would need to repeat it indefinitely without wasting memory), if I use AVLooper I get a silence between loops. All with the same sound. Idk :/

PS2: The same happens with ALAC files.

Post not yet marked as solved Up vote post of franciscorec Down vote post of franciscorec
698 views
  • Im using .flac format because it's the only one not adding a little silence at the start/end of each file like mp3 or m4a and is not heavy like wav.

  • What is the length of the Audio Clip in relation to the number of bars being played and the last instrument or vocal in the music before the end? The Audio clip might need to be re-edited using an audio editing tool ...

  • Hi! it’s an 8 seconds flat white noise that is a perfect loop, I tested it in audacity and in live (where a sound producer made it for me, and tested too) and it plays smoothly in loop. (We tested the exported version of course and the audio file it’s not the problem)

Add a Comment