Audio disposal and errors

Hi @tjed10,

I strongly suggest that you avoid global variables for this. They are ripe for creating problems and it may be why you’re having these issues.

Here are two tutorials on how to avoid them… the second one even focuses specifically on audio implementation:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

http://coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

Take care,

Brent

Well without using global variables, I found that when I toggled sound off, I got errors trying to turn it back on because it was not yet defined in that scene. So then I would define it but it would run into race conditions and cause other errors (like already streaming errors). However, as global variables, I found that I still don’t know when to dispose of them because they become undefined. Doing local variables allowed me to dispose of them between scenes but redefine them in the next.

And while the tutorials are handy (wish I found it before I reworked my variables to global and before I put in about 80 “if sound on, play sound”), they don’t specifically address streaming sounds. It mentions disposing of sounds, but not streams, so it still doesn’t really answer my question of when to dispose of streams (if ever…or simply when you close out of the app or no longer need the song again, like if you beat level 1 and no longer need level 1’s song).

I will say that global variables have simplified my code and caused my errors/warnings to go away. With locally defined variables I got 1 of 4 errors when I began turning sounds on or off or switched scenes. Now the only problem is toggling sound sometimes plays the wrong song.

The “Goodbye Globals” should show you a technique for creating a globally available table of values that don’t occupy global space.  They allow you to have a local access to them.  In fact some people call that table globals (which is fine), as long as you’re not using _G or having variables that are not part of a table or local to the chunk you’re working in.

Rob