Why will my sound not stop?

closed account (367kGNh0)
(This is a c++ question, it just incorporates a framework, if you look at it down to the logic maybe you can help, I have tried all day)

I want to stop my audio, but my attempt is not working. it doesn't pause.

This is what I put in the header file
int Sound;

This is what I did in the cpp file
int Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");

later, in another method in that cpp
cocos2d::experimental::AudioEngine::stop(Sound);

When conditions are met to stop the sound, it did not stop. I implore you for the reason
Last edited on
Try putting extern int Sound; in the header file. You want it to always refer to the Sound variable in that particular cpp file even when referenced from another cpp file. The way you have it it will create a new (different) Sound variable in every cpp file that includes the header.

But then again, you probably would've gotten some linker errors...
Last edited on
closed account (367kGNh0)
If I type extern it instantly says "it's an invalid storage class for a class member"
Last edited on
I was thinking about another situation entirely. Forget what I said.

Instead, it looks like you have an int as a class member that you assign to in one of the member functions. But when you assign to it you are creating a new (different) Sound variable that disappears when it goes out of scope. Then in another member function you are trying to turn the sound off using the value in the original Sound variable, which contains an arbitrary value.

To fix it, just remove the "int" from the assignment statement so you are assigning to the Sound member variable and not a local function variable that is shadowing it.
closed account (367kGNh0)
I'm afraid it did not stop
Is the value of the int returned from cocos2d::experimental::AudioEngine::play2d definitely the same value that you're passing to cocos2d::experimental::AudioEngine::stop?

If so, how do you know? What does your log say?
closed account (367kGNh0)
Wait, it did, sorry, my code is really lengthy, so that was just human error. This is not the end though. We know how to pause the audio, but, ok, let me explain.

context, I want to delay a sound by 0.8 seconds

This was the original bad cpp, more lines of it

1
2
3
4
5
6
7
8
9

	auto sound = []()
	{
        int Sound = cocos2d::experimental::AudioEngine::play2d("Sound.mp3");
	};

	
	auto seq = Sequence::create(DelayTime::create(0.8),           CallFunc::create(sound), nullptr);
	[...]->runAction(seq);



When I edited it with @dutch 's help, I was forced to do this, (to test if it works)


1
2
3
4
5
6
 Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
	auto sound = []()
	{
       
	};


Because unfortunately, If I do

1
2
3
4
5
6

	auto sound = []()
	{
        Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
	};


It will say: "the enclosing funtion 'this' cannot be referenceeeeeeeee... Oh, solved it haha, i just had to do

1
2
3
4
5
6

	auto sound = [this]()
	{
        Sound = cocos2d::experimental::AudioEngine::play2d("sound.mp3");
	};



Last edited on
Topic archived. No new replies allowed.