Playing From Resource (Visual Studio)
C++. If needed to play audio files from resource in Visual Studio (not to directly provide audio files), process would be a little bit different. When upload files to resource, firstly you create .rc file, and then import files into it. After uploading as binary file, a resource ID would be given in resource.h file. You can rename or modify them:
Then, by a piece of code block, you can search the file and use in the play function. FindResource(NULL, MAKEINTRESOURCE(resourceId), resourceType) plays an important role here. Here, play function for FMOD coud be like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void playFromResource(int resource_ID, LPCTSTR resource_type) { HRSRC rsrc = FindResource(NULL, MAKEINTRESOURCE(resource_ID), resource_type); HGLOBAL handle = LoadResource(NULL, rsrc); DWORD audio_size = SizeofResource(NULL, rsrc); LPVOID audio_data = LockResource(handle); FMOD_CREATESOUNDEXINFO AudioInfo; memset(&AudioInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); AudioInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); AudioInfo.length = static_cast<unsigned int>(audio_size); FMOD::Sound* ptrResource; //FMOD::SYSTEM* ptrSystem, it is used at the initialising process ptrSystem->createSound(static_cast<const char *>(audio_data), FMOD_OPENMEMORY, &AudioInfo, &ptrResource); ptrSystem->playSound(ptrResource, nullptr, false, 0); } |
Although it is enough for a steady single play, it is not good enough for interactive music play because it needs quickly search for short segments. It creates approximately 40-50 milliseconds delay for quick loads. That’s why, for optimisation, it would be good to insert sound objects for each file to relevant map container so that play function could call them from there. In my Closed Single State post, it would be easily placed there, and would perfectly work without a delay. You can download an example .exe file and run it. So, loading function, setting file names and load them into the container would be like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | void Interactive::loadFromResource(std::string set_name, int resource_ID, LPCTSTR resource_type) { HRSRC rsrc = FindResource(NULL, MAKEINTRESOURCE(resource_ID), resource_type); HGLOBAL handle = LoadResource(NULL, rsrc); DWORD audio_size = SizeofResource(NULL, rsrc); LPVOID audio_data = LockResource(handle); FMOD_CREATESOUNDEXINFO AudioInfo; memset(&AudioInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO)); AudioInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); AudioInfo.length = static_cast<unsigned int>(audio_size); FMOD::Sound* ptrResource; ptrSystem->createSound(static_cast<const char *>(audio_data), FMOD_OPENMEMORY, &AudioInfo, &ptrResource); //Adding loaded resource file to the map container InteractiveMap.insert(std::make_pair(set_name, ptrResource)); ptrSystem->update(); } //Main cpp //Loading interactive music files from resource //Setting resource name, Resource ID, Resource type audio.loadFromResource("battle1", 201, "MP3"); audio.loadFromResource("battle2", 202, "MP3"); audio.loadFromResource("battle3", 203, "MP3"); audio.loadFromResource("battle4", 204, "MP3"); |
References:
[1] Using Resources – MSDN – Microsoft [2] FMOD API Documentation [3] Katy’s Code