Play reusable sound audioEngine

Hi, i was using simple audio engine for simple stuffs like menu item sound and so…

Now i want to give a try with audioEngine, i see that the play2d function returns an integer, i would like yo reuse that ID to play the same sound again later… But to play the sound again i cannot use play2d because that Will return a new integer…

I was looking into the api and found the resume function, does this function Will play the already assigned sound ID ir is ir intended for other objective?


Is there something in the source code that is confusing with regards to how this works? I’m assuming you have actually inspected the play2d method and related code.

You can’t use the same ID, since it has no link to the actual audio file being played, but instead maps to the instance of the player that is playing that sound (or something like that).

What is your reason for wanting to use the same ID again? Why would you not want to call play2d with the name of the audio file whenever you need it?

Hi thanks for your answer.

Maybe i misunderstood the way that integer returned by play2d works, i was thinking that integer was somehow connected with the file, if not, then i need to change my concept…

If that integer is not controlling the file, so how it controls the volume and pitch of the file using the ID?..

What im looking for is to play a bgm… ( let’s say it returns the ID 5)
In other function later i need to stop that bgm… Then later in other function play another bgm (let say ID 6). Then i stop this last one and want to play the ID 5 again, i can not do that with play2d because it will returns the ID 7 while i want to control ID 5…

In this way i cannot do a map or dictionary of char*, int since int will be different each time y play the same bgm…

Could you please give me an example of the correct use of play2d for my case?

Thanks again

That functionality may be missing from the newer audio engine being used, as it doesn’t seem to be implemented. The back-end (OpenAL and such) does support such functionality, but the AudioEngine doesn’t provide access to them.

You may want to add a management adapter on top of the audio engine, one that handles virtual channels. The virtual channels are tracked by integer IDs. You play a sound on a channel, and internally the channel manager will call play2d, and map a channel ID to the audio ID, and it will return the channel ID to your game code.

All your game code would then simply go through the channel manager, and for example, channel 5 will always be your BGM channel. Internally you don’t care what audio ID it is using, since you’re only working with the channel IDs. All the mapping is handled inside that channel management adapter. Does that make sense?

As an example from one of my applications (you’re free to use this class declaration, but the implementation you’ll need to do yourself):

constexpr uint8_t MaxNumberOfChannels = 32;
constexpr uint8_t MaxNumberOfReservedChannels = 16;

class ChannelManager
    int Play(const std::string& fileName, int channel, bool loop);
    int Play(const std::string& fileName, bool loop);
    void Pause(int channel);
    void Resume(int channel);
    void Stop(int channel);
    float GetDuration(int channel);
    float GetCurrentPosition(int channel);
    float GetPercentPlayed(int channel);
    bool IsPlaying(int channel);
    void ReserveChannels(uint8_t numberOfChannels);
    void SetVolume(int channel, float volume);
    float GetVolume(int channel);
    int GetEmptyChannel();

    std::array<int, MaxNumberOfChannels> _channels{}; // The index is the channel ID, and the stored value is the AudioID returned by the AudioEngine.
    int _numberOfReservedChannels;    
1 Like

that makes sense … Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.