Playing Music - sinusinu/Flora GitHub Wiki
Flora provides Music class for playing music.
Unlike Sound which decodes and stores sample data entirely on memory, Music streams and decodes audio file on the fly, small amount at a time. It makes Music a little bit more heavier than Sound, but it uses much less memory - compared to Sound which goes out of control very quickly with long audio files. Thus, Music is suitable for playing long background music.
Let's take a look!
Begin with Simple Program:
âšī¸ Before starting, place any audio file of compatible format as music.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 {
Music music;
public override void Prepare() {
music = new Music(PathUtils.Relative("music.ogg"));
music.Play();
}
public override void Pause() {
}
public override void Resume() {
}
public override void Resize(int width, int height) {
}
public override void Render(float delta) {
Gfx.Begin();
Gfx.End();
}
public override void Cleanup() {
music.Dispose();
}
}
}As soon as you run the application, music will start playing.
music = new Music(PathUtils.Relative("music.ogg")) creates Music object with music.ogg file.
music.Play() plays the music.
Because Music handles low-level resources, it must be disposed by calling Dispose() function after use.
Music provides below properties:
-
State- Represents the state of this
Musicobject. Read-only. - Either one of
MusicState.Idle,MusicState.Playing,MusicState.Paused.
- Represents the state of this
-
Volume- Gets or sets the volume.
- Ranges between
0fand1f.
-
Looping- Gets or sets if the music should loop.
-
trueorfalse.
Music provides below functions:
-
Play()- Plays the music. If music is paused, playback will resume.
-
Pause()- Pauses the playback. If music is not playing or already paused, nothing will happen.
-
Stop()- Stops the playback. If music is not playing, nothing will happen.
-
float GetPosition()- Gets the current music position.
- Return value is in seconds.
-
SetPosition(float position)- Sets the current music position.
-
positionmust be in seconds.