Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ int main() {
}
```

### Music

raylib streams music directly from the file buffer, so load the file data yourself, keep it alive while the music plays, and free it after unloading the music stream.

``` c
int size;
unsigned char *data = LoadFileDataFromPhysFS("assets/music.ogg", &size);
Music music = LoadMusicStreamFromMemory(".ogg", data, size);
// ... play the music ...
UnloadMusicStream(music);
UnloadFileData(data); // Free the buffer only after unloading the music stream.
```

### API

``` c
Expand All @@ -72,7 +85,6 @@ Image LoadImageAnimFromPhysFS(const char* fileName, int* frames); // Load an an
Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS
Wave LoadWaveFromPhysFS(const char* fileName); // Load wave data from PhysFS
Sound LoadSoundFromPhysFS(const char* fileName); // Load a sound from PhysFS
Music LoadMusicStreamFromPhysFS(const char* fileName); // Load music data from PhysFS
Font LoadFontFromPhysFS(const char* fileName, int fontSize, int *fontChars, int charsCount); // Load a font from PhysFS
Shader LoadShaderFromPhysFS(const char* vsFileName, const char* fsFileName); // Load shader from PhysFS
void SetPhysFSCallbacks(void); // Set the raylib file loader/saver callbacks to use PhysFS
Expand Down
6 changes: 5 additions & 1 deletion examples/audio/audio_music_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ int main(void)
InitPhysFS();
MountPhysFS("resources", "res");

Music music = LoadMusicStreamFromPhysFS("res/country.mp3");
// Load the music file data manually, as the buffer must stay valid while the music plays.
int musicDataSize = 0;
unsigned char *musicData = LoadFileDataFromPhysFS("res/country.mp3", &musicDataSize);
Music music = LoadMusicStreamFromMemory(".mp3", musicData, musicDataSize);

PlayMusicStream(music);

Expand Down Expand Up @@ -90,6 +93,7 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadMusicStream(music); // Unload music stream buffers from RAM
UnloadFileData(musicData); // Free the music file data after the stream is unloaded

CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)

Expand Down
30 changes: 0 additions & 30 deletions raylib-physfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ RAYLIB_PHYSFS_DEF Image LoadImageAnimFromPhysFS(const char* fileName, int* frame
RAYLIB_PHYSFS_DEF Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS
RAYLIB_PHYSFS_DEF Wave LoadWaveFromPhysFS(const char* fileName); // Load wave data from PhysFS
RAYLIB_PHYSFS_DEF Sound LoadSoundFromPhysFS(const char* fileName); // Load a sound from PhysFS
RAYLIB_PHYSFS_DEF Music LoadMusicStreamFromPhysFS(const char* fileName); // Load music data from PhysFS
RAYLIB_PHYSFS_DEF Font LoadFontFromPhysFS(const char* fileName, int fontSize, int *fontChars, int charsCount); // Load a font from PhysFS
RAYLIB_PHYSFS_DEF Shader LoadShaderFromPhysFS(const char* vsFileName, const char* fsFileName); // Load shader from PhysFS
RAYLIB_PHYSFS_DEF void SetPhysFSCallbacks(void); // Set the raylib file loader/saver callbacks to use PhysFS
Expand Down Expand Up @@ -495,35 +494,6 @@ Sound LoadSoundFromPhysFS(const char* fileName) {
return sound;
}

/**
* Load module music from PhysFS.
*
* @param fileName The file name to load from the PhysFS mount paths.
*
* @return The Music object, or an empty Music object on failure.
*
* @see UnloadMusic()
*/
Music LoadMusicStreamFromPhysFS(const char* fileName) {
int bytesRead;
unsigned char* fileData = LoadFileDataFromPhysFS(fileName, &bytesRead);
if (bytesRead == 0) {
Music output = { 0 };
return output;
}

// Load from the memory.
const char* extension = GetFileExtension(fileName);
Music music = LoadMusicStreamFromMemory(extension, fileData, bytesRead);

// Unload the file data if the music failed to load.
if (music.ctxData == NULL) {
UnloadFileData(fileData);
}

return music;
}

/**
* Load font from PhysFS.
*
Expand Down
Loading