7

I'm using the SKTAudio library given by Ray Wenderlich on his Github. Here's the code I've modified for playing a sound effect that I call everytime I play one:

public func playSoundEffect(_ filename: String) {
    let url = Bundle.main.url(forResource: filename, withExtension: nil)
    if (url == nil) {
        print("Could not find file: \(filename)")
        return
    }
    if !UserDefaults.standard.bool(forKey: "soundOff"){
    var error: NSError? = nil
    do {
        soundEffectPlayer = try AVAudioPlayer(contentsOf: url!)
    } catch let error1 as NSError {
        error = error1
        soundEffectPlayer = nil
    }
    if let player = soundEffectPlayer {
        DispatchQueue.global(qos: .background).async {
            player.numberOfLoops = 0
            player.prepareToPlay()
            player.play()
        }
    } else {
        print("Could not create audio player: \(error!)")
    }
    }
}

I'm using this because this way I can mute the sound effects easily, since this is using a singleton class method. The thing is, this produces minor lag everytime I play a sound effect. If I mute the game, no lag. I tried making the sound play in a background thread, but the result is the same.

Is there any easy change to this code to make it have NO LAG?

EDIT: This is not a duplicate because none of the other answers on other questions solved my problem.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
Pedro Cabaco
  • 165
  • 1
  • 11
  • There are multiple threads on this,have you checked them first? And have you tried out the solutions? What didn't work and what did you try from those answers? http://stackoverflow.com/questions/5148348/avaudioplayer-lag-when-calling-play http://stackoverflow.com/questions/25503629/avaudioplayer-produces-lag-despite-preparetoplay-in-swift http://stackoverflow.com/questions/2550480/delay-in-playing-sounds-using-avaudioplayer http://stackoverflow.com/questions/5684374/trying-to-fix-avaudioplayer-lag-on-its-initial-use http://stackoverflow.com/questions/26345857/avaudioplayer-causes-game-lag –  Feb 25 '17 at 22:01
  • Possible duplicate of [Sound causing game to lag in swift sprite kit game?](http://stackoverflow.com/questions/41180669/sound-causing-game-to-lag-in-swift-sprite-kit-game) –  Feb 25 '17 at 22:01
  • http://stackoverflow.com/questions/27218257/avaudioplayer-causes-my-game-to-lag http://stackoverflow.com/questions/38402462/game-lagging-when-avaudioplayer-plays http://stackoverflow.com/questions/31840081/play-sound-with-a-little-delay etc... –  Feb 25 '17 at 22:02
  • http://stackoverflow.com/questions/38042613/avaudioplayer-and-performance-issue-in-spritekit-game –  Feb 25 '17 at 22:02
  • @Sneak yes I've done my research before asking and these do not solve my problem. The only thing that seems to me that came close to solving was calling .prepareToPlay() in the ViewDidLoad() method. Still not perfect. – Pedro Cabaco Feb 25 '17 at 22:27
  • I researched and I know that a fix is to play audio as actions, but I want to play audio using AVAudioPlayer. And my question is if it is possible, from the above code, to do so with AVAudioPlayer with no lag – Pedro Cabaco Feb 25 '17 at 22:53
  • This code you have reported is not the [`STKAudio`](https://github.com/raywenderlich/SKTUtils/blob/master/SKTUtils/SKTAudio.swift), because there is no need to create a background queue as you doing with `DispatchQueue.global(qos: .background).async`, you should play your audio to the main thread. – Alessandro Ornano Feb 26 '17 at 08:26
  • @AlessandroOrnano as I said in my question, I tried to use a background thread, as suggested in another answer, but it didn't work. It was the same as playing in the main thread. – Pedro Cabaco Feb 26 '17 at 14:47
  • Apparently my question is incorrect, I don't get why people downvote my question... can anyone tell me what I did wrong? If it's wrong then why does nobody give an answer....? Jesus... – Pedro Cabaco Feb 26 '17 at 14:48
  • `AVAudioPlayer` don't cause lag. Try to use another little file as an mp3 of 96 Kbps to make experiments: if you have always lags maybe you should control your code to see if you are really on the main thread during playing. – Alessandro Ornano Feb 26 '17 at 15:25
  • 1
    Since I'm playing really short sound effects, it produces lag. If I play it either in the background or main thread, there's some annoying lag going. There should be a simple solution to this. – Pedro Cabaco Feb 26 '17 at 15:27
  • Have you test this on simulator or on device? Another advice: try to leave untouched the third part library as `SKTAudio`, you should improve their code to your project using for example extensions. In this way, you can follow third part libraries updates without re-write the intere code. – Alessandro Ornano Feb 26 '17 at 15:31
  • Tested on device of course. How would I pre load the audio files before playing them so there's no lag? – Pedro Cabaco Feb 26 '17 at 15:49
  • For simple sound effects in `SpriteKit` I just use `SKAction.playSoundFileNamed` you can also preload the action by creating an SKAction var that you can use later. – rmp Feb 28 '17 at 19:16

0 Answers0