Community Q&A

Where Wwise users help each other out!

Audiokinetic's Community Q&A is the forum where Wwise users ask and answer questions within the Wwise community. If you would like to get an answer from Audiokinetic's Technical support team, make sure you use the Support Tickets page.

Check if a Event is playing

+2 votes
Hi All,

 

can someone explain me how to check if a Event is playing in Unity? We're using Unity 2018.1.1 and correlated Wwise integration. I can't find how to check this situation.

Kind regards
asked Dec 7, 2018 in General Discussion by Loris C. (140 points)
I just came here to ask the same question after searching hours for an answer.  Weird you can't find basic stuff like this in the docs, or anywhere else.
Since there seems to be no answer to this question, I had to implement my own state flags.  The looping events are easy enough by setting an extra flag when posting and stopping.  The one-offs are a bit more involved requiring a callback on EndOfEvent just to turn the flag off.  I suppose I could create a wrapper class if I really wanted to.  But, very very strange I have to add play state to someone else's audio object.  Isn't that what people want to know first about audio?
Hope no, because for my project make a state machine that will check if the events are playing will be very pity and frustrating (we're making a comic book app and some events plays for many pages and others only for one). Maybe, as i was thinking with my developer, wwise manage it in a very different way but it's not clear from the documentation.

1 Answer

0 votes

I got some help in a Facebook group on how to use GetPlayingIDsFromGameObject and here is what I came up with to test if an event is playing on a certain GameObject. It's tricky because GetPlayingIDsFromGameObject needs an array of exactly the right size in order to work (for whatever reason), hence why we call that function twice. Creating a new array every time you call the function is a bit concerning. Anywho, hope this is useful to someone.

 

public static bool IsEventPlayingOnGameObject(string eventName, GameObject go)
{
    uint testEventId = AkSoundEngine.GetIDFromString(eventName);

    uint count = playingIds.length;
    result = AkSoundEngine.GetPlayingIDsFromGameObject(go, ref count, playingIds);

    for (int i = 0; i < count; i++)
    {
        uint playingId = playingIds[i];
        uint eventId = AkSoundEngine.GetEventIDFromPlayingID(playingId);

        if (eventId == testEventId)
            return true;
    }

    return false;
}

EDIT: I updated the code snippet.

After playing around some more and re-reading the documentation, I discovered that it is not necessary to have an array of precisely the correct length. The important thing is that count is both an input and an output from the function. count should not be 0 but rather be the length of the array playingIds. after the function returns, count will be the number of ids plopped into playingIds. No need to call the function twice or make a new array each time the function is called.

So the mistake was setting count=0 which told the function that our array had a length of 0 and therefore it couldn't plop any ids into it.

I think my confusion stemmed from the difference in conventions between C++ and C#. In C++ the array is a pointer and doesn't pass any information about its length. In C# you can always get the length of an array, which is why this seems like a weird way to do things.

answered Mar 18 by Simon S. (180 points)
edited Mar 19 by Simon S.
...