-
Notifications
You must be signed in to change notification settings - Fork 55
3. Playing Your Sounds
This guide assumes that you have set up AudioManager and have generated it's Audio Library already. If you'd like to bring yourself up to speed, you can read up on Downloading and Importing JSAM and Getting Started with JSAM
There are several ways you can play sounds using JSAM. The most basic way is to make use of JSAM's various sound playing components.
An AudioPlayer does exactly what it's name suggests. They're quick to deploy and easy to work with! You can either spawn them as standalone GameObjects or you can add them as a component to an existing GameObject.
For now, right-click in the hierarchy window, hover over JSAM, and click on Audio Player to add a basic Audio Player object to the scene.

If you look at the Audio Player, you can see that it should have already selected a default sound to play. Just below that where it says Audio Player Settings are a series of check-boxes with the boxes labelled Play on Start and Stop on Disable ticked-in. If you start the scene, the sound will play immediately, as per the Play on Start option. If your sound was sufficiently long enough (or if you ticked the Loop Sound option), the sound may play long enough for you to disable the component or the GameObject itself, thus stopping the sound.
The AudioPlayer contains a public method Play() that you can call if you have a reference to it through script, in-case you'd like the Audio Player to play at your whim. Otherwise, just enabling/disabling the script will be enough to play it's designated sound in most cases.
JSAM was made to be decoupled from the rest of your game code. So all you really have to do is type one line of code to play your sound!

Don't forget to add using JSAM; at the top of your script if you're planning to use make JSAM calls often! It saves you having to add JSAM. in front of every little thing.
When you generated your Audio Files in AudioManager, it also generated a C# script containing enums meant to aid your sound playing ventures! These enums are meant to be passed into AudioManager's methods as parameters. The enums are generated using the name of your Audio File Object so remember to regenerate your Audio Library when you change the name of an Audio File Object.
Note: AudioManager.PlaySound only works if AudioManager exists in your scene!
If you want your sound to play all 3D-like in your listener's left/right speakers, you'll first have to make sure your Audio File Object has Spatialize ticked on.

After that, return to your code where you wrote the PlaySound method. From there, insert a reference to the Transform component of the GameObject you want your sound to follow.
In this non-animated example image, the listener will perceive the sound at the position of the GameObject the script is attached to.

Wherever the GameObject goes, the sound follows suit (so long as the sound is playing).
Note: This will only work if the Audio File Object has Spatialize ticked on AND you have passed a reference to a Transform in the PlaySound method. If either of these aren't done, the sound will play as if it's right on top of the listener.
If your sound enum is a bit difficult to type, you can copy the entire enum name to your clipboard by opening AudioManager's audio library view, right-clicking on an entry and selecting the Copy to Clipboard option. Happy pasting!

AudioManager.PlaySound returns the AudioSource that's actually playing your sound behind the scenes! So if you wanted to work your own funky magic on your sound while it's being played, then you will always have that option!

Note: The AudioSource is still being used by AudioManager and when your sound is finished playing, the AudioSource will be recycled to play a different sound.