Cannot play music, SimpleAudioEngine.h not accessible

I am trying to put some music on my project, but it won’t allow me to access SimpleAudioEngine.h, I keep getting this error:

Error (active) “CocosDenshion::SimpleAudioEngine::SimpleAudioEngine()” (declared at line 256 of “c:\MyGame \cocos2d\cocos\audio\include\SimpleAudioEngine.h”) is inaccessible MyGame c:\MyGame \Classes\MyGame .cpp

I have included the header and namespace properly, I’m using visual studio and it showed me the definition so It knows where the file is, I have no idea what’s the problem.

Permissions on the .h?

Good idea but I just checked it and that’s not the problem,

Ok, VS is not happy about SimpleAudioEngine because it has it’s default constructor set to protected if I change it to public Visual Studio will compile it without error, but it does not work, because no music plays in the background.

What vers. of cocos are you using?

cocos2d-x-3.13

Alright, after I noticed that getInstance is a static function and without modifying the SimpleAudioEngine files, I was able to get the Instance of SimpleAudioEngine, by using CocosDenshion::SimpleAudioEngine::getInstance() instead of the mistake I was doing of CocosDenshion::SimpleAudioEngine(), but I was able to access it by removing the protected keyword, in either case.

The only issue is that it’s not playing the background music. I noticed that some virtual function like preloadBackgroundMusic, setBackgroundMusicVolume and a few other are not implemented.

not implemented? Just on Win32?

So far I’ve compiled it on Win32, Win10 and Win8.1 and all of them don’t have bgm playing.

Could you post a code snippet? Maybe you missed something obvious. I’ve used SimpleAudioEngine in win32 projects and never had problems with background music. Maybe the music format?

I’ve tried both .wav and .ogg, none of them worked.

bool MyGame::init()
{
	if (!Layer::init())
	{
		return false;
	}
	is_dragged = false;

	CCLOG("Starting Music");
	const char* MUSIC_PATH = "Music/MainThemeLoop.ogg";
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(MUSIC_PATH);
	CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(MUSIC_PATH, true);

	initTouch();
	initTiled();
	
	this->scheduleUpdate();
	return true;
}

I don’t think it’s mandatory to preload background music, try playing directly, and try with a small mp3 file.

1 Like

Thanks it worked, when I switched to .mp3 file.