#pragma once #ifdef __ANDROID__ #include #include #else #include #include #endif #include #include #include #include #include #ifndef __EMSCRIPTEN__ #include #include #include #include #endif class AudioPlayerAsync { public: AudioPlayerAsync(); ~AudioPlayerAsync(); bool init(); void shutdown(); void playSoundAsync(const std::string& filePath, int loops = 0, int channel = -1); void playMusicAsync(const std::string& filePath, int loops = -1); void crossFadeMusicAsync(const std::string& filePath, int fadeDurationMs = 1000, int loops = -1); void crossFadeMusicFromPositionAsync(const std::string& filePath, int fadeDurationMs = 1000, int loops = -1); void stopMusicAsync(); void pauseMusicAsync(); void resumeMusicAsync(); void setMusicVolume(int volume); // 0..128 void setSoundVolume(int volume); // 0..128 int getMusicVolume() const { return musicVolume_; } int getSoundVolume() const { return soundVolume_; } void setMusicEnabled(bool enabled); void setSoundEnabled(bool enabled); bool isMusicEnabled() const { return musicEnabled_; } bool isSoundEnabled() const { return soundEnabled_; } void exit() { stop = true; } private: #ifndef __EMSCRIPTEN__ void workerThread(); std::thread worker; std::mutex mtx; std::condition_variable cv; std::queue> taskQueue; #endif bool stop = false; std::unordered_map soundCache; std::mutex soundCacheMutex; std::string currentTrack; Mix_Music* currentMusic_ = nullptr; std::vector currentMusicData_; int musicVolume_ = 128; int soundVolume_ = 128; bool musicEnabled_ = true; bool soundEnabled_ = true; bool initialized = false; };