JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
Channel Class Reference

An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects". More...

#include <jngl/Channel.hpp>

Public Member Functions

void play (const std::string &filename)
 Play OGG file on this channel.
 
void loop (const std::string &filename)
 Play an OGG audio file on this channel in a loop. More...
 
void stop (const std::string &filename)
 Stop OGG file playing on this channel.
 
Finally pause ()
 Pauses the Channel; destroying the returned Finally object will resume again. More...
 
void add (std::shared_ptr< Stream >)
 Internal function for now.
 
void remove (const Stream *)
 Internal function for now.
 

Static Public Member Functions

static Channelmain ()
 Returns the main Channel on which jngl::play, jngl::loop, jngl::stop operate.
 

Detailed Description

An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects".

Example:

struct Channels : public jngl::Singleton<Channels> {
jngl::Channel speech;
};
// somewhere else:
Channels::handle().music.loop("background_music01.ogg");
An audio channel, different channels could be for example: "Music", "Speech" and "Sound Effects".
Definition: Channel.hpp:28
static Channel & main()
Returns the main Channel on which jngl::play, jngl::loop, jngl::stop operate.
void loop(const std::string &filename)
Play an OGG audio file on this channel in a loop.
Inherit from this class to create a singleton that will be destroyed when your games exits.
Definition: Singleton.hpp:23

Definition at line 28 of file Channel.hpp.

Member Function Documentation

◆ loop()

void loop ( const std::string filename)

Play an OGG audio file on this channel in a loop.

If it's already playing, this function won't play it twice.

◆ pause()

Finally pause ( )

Pauses the Channel; destroying the returned Finally object will resume again.

Note that this doesn't change the status of jngl::isPlaying as that only depends on the status of the SoundFile.

Example where you want to pause audio in a menu:

class PauseMenu : public jngl::Work {
jngl::Finally paused;
public:
PauseMenu() : paused(jngl::Channel::main().pause()) {}
void step() override { /* ... */ }
void draw() const override { /* ... */ }
};
Helper class which calls a function when being destroyed.
Definition: Finally.hpp:22
Active state of the game, e.g. a menu or the game itself.
Definition: work.hpp:13
JNGL's main namespace.
Definition: Achievement.hpp:10

Or if you want to toggle pausing audio using Space:

class PauseExample : public jngl::Work {
public:
void step() override {
if (jngl::keyPressed(jngl::key::Space)) {
if (paused) {
paused = {}; // calls ~Finally() and resumes playback
} else {
paused = jngl::Channel::main().pause();
}
}
}
void draw() const override { /* ... */ }
};
Finally pause()
Pauses the Channel; destroying the returned Finally object will resume again.
bool keyPressed(key::KeyType key)
Whether key has been pressed since the next to last call to updateInput()

The documentation for this class was generated from the following file: