Playing Sound - sinusinu/Flora GitHub Wiki

Flora provides Sound class for playing sound effects.

Unlike Music which loads and decodes audio file small amount at a time, Sound decodes and stores sample data entirely on memory. It makes Sound use much more memory - especially on long sounds, but it is much more responsive and faster. Thus, Sound is suitable for playing fire-and-forget type of short sound clips.

Let's take a look!

Playing Sound

Begin with Simple Program:

ℹī¸ Before starting, place any audio file of compatible format as sound.ogg (if format is different, change extension in code)
Compatible audio formats are: WAV, MP3, OGG.

using Flora;
using Flora.Audio;
using Flora.Gfx;
using Flora.Util;

namespace FloraTest {
    class MyCore : FloraCore {
        Sound sound;
        float soundTimer = 0f;

        public override void Prepare() {
            sound = new Sound(PathUtils.Relative("sound.ogg"));
        }

        public override void Pause() {

        }

        public override void Resume() {

        }

        public override void Resize(int width, int height) {

        }

        public override void Render(float delta) {
            soundTimer += delta;
            if (soundTimer > 2f) {
                sound.Play();
                soundTimer %= 2f;
            }

            Gfx.Begin();
            Gfx.End();
        }

        public override void Cleanup() {
            sound.Dispose();
        }
    }
}

When you launch this application, Sound effect will play every 2 seconds.

sound = new Sound(PathUtils.Relative("sound.ogg")) creates Sound object with sound.ogg file.

sound.Play() plays the sound.

Because Sound handles low-level resources, it must be disposed by calling Dispose() function after use.

Inside Sound

Sound provides below property:

  • Volume
    • Gets or sets the volume.
    • Ranges between 0f and 1f.

Sound provides below function:

  • Play(bool singleton)
    • Plays the sound. if singleton is true, any of this sound playing currently will be halted before playing.

Notice the lack of *Position functions: Sound is not capable of seeking audio data. If you need to seek the audio data, use Music.

⚠ī¸ **GitHub.com Fallback** ⚠ī¸