Loading sounds assets

I have a game and I'm wondering where is the best place to load sounds.

Right now I'm using the didMoveTo... function of my game scene but the game is short; so the user is constantly going into this scene. I think the sounds should only be loaded once instead of every time the scene loads, maybe in the AppDelegate? and if so, how would I reference to them from the scene?

Thanks!

Accepted Reply

If the same scene is being accessed all the time, you're right, it's not gonna be very optimized to load the audio assets in that scene upon loading. You could load them all in GameViewController in a variable or object that's globally available.


I've done this is in the past so that I have a completely separate Audio class, that has the storage of audio samples in variables, methods to load them and methods to play them. So when GameViewController is loaded, I declare a globally-accessible audio property of class Audio, run its load methods, then when I want some sound to play, I run the play methods. This way you can isolate your audio system, including framework imports, etc., to just one place, making it easy to change things universally.

Replies

If the same scene is being accessed all the time, you're right, it's not gonna be very optimized to load the audio assets in that scene upon loading. You could load them all in GameViewController in a variable or object that's globally available.


I've done this is in the past so that I have a completely separate Audio class, that has the storage of audio samples in variables, methods to load them and methods to play them. So when GameViewController is loaded, I declare a globally-accessible audio property of class Audio, run its load methods, then when I want some sound to play, I run the play methods. This way you can isolate your audio system, including framework imports, etc., to just one place, making it easy to change things universally.

hello Pintxo,


Thanks for your reply, I think that is a very good idea but have a few more questions:

  • So I have created my Audio class with all the variables and methods
  • In the GameViewController, I create an instance of this class when my controller loads
  • My question is then how do I reference to the audio object from my scene? Like you said, this has to be a global var.. sorry I'm a bit new to programming.


Thanks for your help!

Swift sort of automatically keeps everything accessible if declared at whatever level you need it to be accessed. So if you have an object of class Audio, you just declare it at the root level outside of scenes and classes and it works like a singleton. What I do is I have my global variables in a file called globals.swift, which is part of the project. I declare those global variables there without a class structure as implicit optionals and then assign them values in GameViewController. I do the same thing with settings, where I list configuration values for my app that don't get rewritten.


Some people might do globals with a separate singleton class and settings in a property list that you fetch, but I haven't found any negative points in my system. The only problem I would recognize is that all of these properties, whether they're optionals or valued, are driven into memory when the app starts, so if there were some huge elements that aren't being used all the time, it would be more performant to only drive those into memory when needed.


Globals.swift

import SpriteKit

var audio: Audio!


GameViewController.swift

import UIKit
import SpriteKit

class GameViewController: UIViewController {
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        audio = Audio()
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
   
    override var prefersStatusBarHidden : Bool {
        return true
    }
}

Thanks Pintxo! your answered helped me get this sorted out and my app is now working better (more efficiently).


Regards!

I use structs and shared singleton, classes seem to have too much latency

I'm having some audio latency and syncing issues with an app I'm working on right now, and I'm trying to figure out what the best system is to implement this. Basically I'm running multiple samples at the same time (like a sampler), but there is noticeable lag once in a while. I'm using AVAudioEngine, but only with AVAudioPlayer elements. I haven't yet tried the actual AVAudioEngine with the buffers and stuff. I would be extremely interested in learning if using structs and a proper singleton would make things faster.


If you can share anything about how you have done things, that'd be great 🙂!