Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music not getting IsPlaying() updated correctly #144

Open
Ismaw34 opened this issue Oct 14, 2021 · 2 comments
Open

Music not getting IsPlaying() updated correctly #144

Ismaw34 opened this issue Oct 14, 2021 · 2 comments

Comments

@Ismaw34
Copy link

Ismaw34 commented Oct 14, 2021

When loading 2 raylib::Music files, and waiting on a if (!currentMusic.IsPlaying()) to switch to music2 using the same pointer. The Music file does not update correctly.

Draft example:
raylib::Music* currentMusic, music1, music2;
music1 = new raylib::Music("1.ogg");
music1.SetLooping(false);
music2 = new raylib::Music("2.ogg");
currentMusic = music1;
currentMusic.Play();
while (running) {
currentMusic.Update();
if (!currentMusic.IsPlaying()) {
currentMusic = music2;
}

Is an example to be filled on the app example. On my case, if I reload the App or make the songs reload on a key press, they dont change and the music1 keeps playing on loop.
Tried with the base library, works as intended. Tried with bindings, fails on this case.
I dont know if the issue is with an operator= when reasigning pointers.

@RobLoach
Copy link
Owner

music1 is a Music pointer, and so you will have to use music1->SetLooping(false) instead?

@Ismaw34
Copy link
Author

Ismaw34 commented Feb 8, 2022

Tried that too, if the music is not playing it gets looped anyways.
This is the code im using.


#include <raylib-cpp.hpp>

#include "ResourceManager.h"

class MusicEngine
{
public:
	static void UpdateMusicEngine() {
		if (currentlyPlaying) {
			if (loopFix && !currentlyPlaying->GetResource()->GetTimePlayed()) {
				currentlyPlaying->GetResource()->Stop();
			}
			if (currentlyPlaying->GetResource()->GetLooping() && !currentlyPlaying->GetResource()->GetTimePlayed()) {
				currentlyPlaying->GetResource()->Seek(loopFrame);
				currentlyPlaying->GetResource()->Update();
			}
			if (currentlyPlaying->GetResource()->IsPlaying()) {
				loopFix = !currentlyPlaying->GetResource()->GetLooping();
			}
			else if (nextPlaying) {
				currentlyPlaying->RemoveUsage();
				currentlyPlaying = nextPlaying;
				nextPlaying = 0;
				currentlyPlaying->GetResource()->SetVolume(max_volume);
				currentlyPlaying->GetResource()->Play();
				currentlyPlaying->GetResource()->Update();
				loopFix = false;
				return;
			}
			if (fading) {
				float c_volume = current_volume;
				if (c_volume == 0.f) {
					fading = false;
					current_volume = max_volume;
					currentlyPlaying->RemoveUsage();
					currentlyPlaying = nextPlaying;
					nextPlaying = 0;
					currentlyPlaying->GetResource()->SetVolume(max_volume);
					currentlyPlaying->GetResource()->Play();
					currentlyPlaying->GetResource()->Update();
					loopFix = false;
					return;
				}
				c_volume -= delta_volume;
				if (c_volume <= 0.f) {
					currentlyPlaying->GetResource()->SetVolume(0.f);
					current_volume = 0.f;
				}
				else {
					currentlyPlaying->GetResource()->SetVolume(c_volume);
					current_volume = c_volume;
				}
			}
			currentlyPlaying->GetResource()->Update();
		}
		else if (nextPlaying) {
			currentlyPlaying = nextPlaying;
			nextPlaying = 0;
			currentlyPlaying->GetResource()->SetVolume(max_volume);
			currentlyPlaying->GetResource()->Play();
			currentlyPlaying->GetResource()->Update();
			loopFix = false;
			return;
		}
	}
	static void PlayNextFade(ResourceItem<raylib::Music*>* newSong) {
		if (fading) {
			if (nextPlaying) {
				nextPlaying->RemoveUsage();
				nextPlaying = newSong;
				newSong->AddUsage();
			}
			else {
				nextPlaying = newSong;
				newSong->AddUsage();
			}
		}
		else {
			if (!currentlyPlaying) {
				PlayNextNow(newSong);
			}
			else if (currentlyPlaying != newSong) {
				fading = true;
				nextPlaying = newSong;
				nextPlaying->AddUsage();
			}
		}
		loopFrame = 0;
	}
	static void PlayNextNow(ResourceItem<raylib::Music*>* newSong) {
		fading = false;
		if (nextPlaying) {
			nextPlaying->RemoveUsage();
			nextPlaying = 0;
		}
		if (currentlyPlaying) {
			currentlyPlaying->GetResource()->Stop();
			currentlyPlaying->RemoveUsage();
		}
		currentlyPlaying = newSong;
		currentlyPlaying->AddUsage();
		currentlyPlaying->GetResource()->SetVolume(max_volume);
		currentlyPlaying->GetResource()->Play();
		currentlyPlaying->GetResource()->Update();
		loopFix = false;
		loopFrame = 0;
	}
	static void PlayNextFinish(ResourceItem<raylib::Music*>* newSong) {
		if (currentlyPlaying) {
			currentlyPlaying->GetResource()->SetLooping(false);
		}
		else {
			PlayNextNow(newSong);
			return;
		}
		if (nextPlaying) {
			nextPlaying->RemoveUsage();
			nextPlaying = 0;
		}
		nextPlaying = newSong;
		nextPlaying->AddUsage();
		nextPlaying->GetResource()->SetVolume(max_volume);
		loopFrame = 0;
	}
	static void StopMusic() {
		if (currentlyPlaying) {
			currentlyPlaying->GetResource()->Stop();
			currentlyPlaying->RemoveUsage();
			currentlyPlaying = 0;
			loopFrame = 0;
		}
	}
	static void SetLoopSecond(float seconds) {
		loopFrame = seconds;
	}
private:
	static inline float max_volume = 0.3f;
	static inline float current_volume = max_volume;
	static inline float delta_volume = 0.02f;
	static inline bool fading = false;
	static inline ResourceItem<raylib::Music*>* currentlyPlaying;
	static inline ResourceItem<raylib::Music*>* nextPlaying;
	static inline bool loopFix = false;
	static inline float loopFrame = 0.f;
};

The Resource manager gets a Object* to the object, i know its not quite elegant, but im improving it.
The this is, even when the next song is queued, the first song is looped even when marking it as false.
The code contains the fixloop if you coment the ocurrences, the thing is happening. For sure is something in my code i dont see or Pointer managing that is wrong on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants