# Audio Loop Lag in XCode

**URL:** <https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673>\
**Category:** iOS\
**Created:** [July 15, 2018, 6:39pm UTC](https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673 "2018-07-15T18:39:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carverparkes](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/carverparkes/32/79544_2.png) [@carverparkes](https://forums.kodeco.com/u/carverparkes)\
**Post date:** [July 15, 2018, 6:39pm UTC](https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673/1 "2018-07-15T18:39:43Z")

</div>

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()  
    }

```

---

<div class="post-metadata">

**Author:** ![carverparkes](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/carverparkes/32/79544_2.png) [@carverparkes](https://forums.kodeco.com/u/carverparkes)\
**Post date:** [July 15, 2018, 7:01pm UTC](https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673/2 "2018-07-15T19:01:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [July 16, 2018, 3:42pm UTC](https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673/3 "2018-07-16T15:42:07Z")

</div>

Hi carverparkes,  
glad that you could resolve this

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [December 29, 2018, 10:41am UTC](https://forums.kodeco.com/t/audio-loop-lag-in-xcode/44673/4 "2018-12-29T10:41:29Z")

</div>

Closed on December 29, 2018, 10:41am UTC

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