Audio Loop Lag in XCode

Hello All,
I am new to programming and I have decided to start by learning Swift. Just wanted to get that out here in the beginning.

My Issue:
I have a looping background audio playing in my app. I created the audio sample in Adobe Audition CC. When I play the audio file in Audition or Audacity the loop plays perfectly without any lag. Now that I have loaded the mp3 file into XCode and run the app in the simulator there is a lag in the loop. So I loaded the app onto my iphone to see if the issue was with the simulator and the lag in the loop was still there.

Can anyone tell me:

  1. Why XCode is creating the lag?
  2. How do I remove the lag from my project?

I am using the following code for the audio:

    var BackgroundAudio:AVAudioPlayer = AVAudioPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.      
        let FileLocation1 = Bundle.main.path(forResource: "mixdown", ofType: "mp3")
        do {
            BackgroundAudio = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation1!))
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
            try AVAudioSession.sharedInstance().setActive(true)
        }
        catch {
            print(error)
        }
        BackgroundAudio.numberOfLoops = -1
        BackgroundAudio.currentTime = 0.0
        BackgroundAudio.volume = 0.07
        BackgroundAudio.prepareToPlay()
        BackgroundAudio.play()  
    }

fixed it. Would have never guessed. File Type. Lag was due to using .mp3 converted file to .wav and no more lag.

Hi carverparkes,
glad that you could resolve this

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.