Skip to content

IAudioPlayer#

Interface for audio playback.

Header:#include <AUI/Audio/IAudioPlayer.h>
CMake:aui_link(my_target PUBLIC aui::audio)

Detailed Description#

Experimental Feature

This API is experimental. Experimental APIs are likely to contain bugs, might be changed or removed in the future.

One IAudioPlayer plays one sound. Create it with IAudioPlayer::fromUrl or IAudioPlayer::fromStream; the returned object is backed by the native audio API of the current platform.

auto player = IAudioPlayer::fromUrl(":sound/click.ogg");
player->setVolume(128);
player->play();

Playback state is changed with play, pause and stop. Use setLoop to repeat the sound and setVolume to change the volume (0 to 256, 256 is the original volume). The finished signal is emitted when the sound ends.

Several players can play simultaneously; their output is mixed by AAudioMixer.

See Playing sounds and Custom sound sources for more.

Warning

A player created from a stream can't be restarted after it has ended or has been stopped, and can't loop: restarting requires reopening the source, which is only possible when the player knows the url. Create a new player instead, or use fromUrl.

Examples#

examples/ui/views/src/ExampleWindow.cpp

Views Example — All-in-one views building example.

            "Common");

#if !AUI_PLATFORM_EMSCRIPTEN
        // [audio_from_url]
        mWavAudio = IAudioPlayer::fromUrl(":sound/sound1.wav");
        mOggAudio = IAudioPlayer::fromUrl(":sound/sound1.ogg");
        // [audio_from_url]

        it->addTab(
            AScrollArea::Builder().withContents(std::conditional_t<

Public Types#


PlaybackStatus#

enum class IAudioPlayer::PlaybackStatus

Playback status depends on last called function among play(), pause(), stop().

Constant Description
PlaybackStatus::PLAYING
PlaybackStatus::PAUSED
PlaybackStatus::STOPPED

Public fields and Signals#


finished#

emits<> finished

On playback finished.


read#

emits<> read

On sound stream read.

Public Methods#

loop#


bool IAudioPlayer::loop()
Returns
Current loop flag

onFinished#


void IAudioPlayer::onFinished()

Called By AAudioMixer when the playback is finished.

See IAudioPlayer::finished for listening for this event.

pause#


void IAudioPlayer::pause()

Pauses audio playback keeping playback progress.

play#


void IAudioPlayer::play()

Starts audio playback, if playback was previously paused, it will continue from where it was paused.

playbackStatus#


PlaybackStatus IAudioPlayer::playbackStatus()
Returns
Current playback status.

resamplerStream#


AOptional<AAudioResampler>& IAudioPlayer::resamplerStream()

Get resampled stream for playback.

setLoop#


void IAudioPlayer::setLoop(bool loop)

Set loop flag, is loop flag is true then audio playback wouldn't be stopped after it ends and sound stream would be rewind.

Arguments
loop
New loop flag

setVolume#


void IAudioPlayer::setVolume(aui::audio::VolumeLevel volume)

Set level of volume.

Arguments
volume
Integer number from 0 to 256 inclusively, works linear

stop#


void IAudioPlayer::stop()

Pauses audio playback without keeping playback progress.

volume#


aui::audio::VolumeLevel IAudioPlayer::volume()
Returns
Current volume level.