- Home /
[SOLVED] audio background music making buzzing sound
Hello everyone! I am working on a project, I added a couple of scenes (Main and Extras are some of them). I have a background music I set on the Menu scene (first scene) as DontDestroyOnLoad so it will keep playing continuously on the other scenes. However in the Extras scene a button that set to play an audio clip, at the beginning the button is disabled. From the Menu when I click on the Extras, the background music keeps working great and the button is disabled. I set it so after I finish the game, the button becomes interactive (or enabled).
After the game is finished, it loads another screen (game over scene). At that scene, the player clicks on (Back) button to return to the Menu scene.
THE PROBLEM IS WHEN I RETURN TO THE MENU SCENE, I THEN CLICK ON THE EXTRAS BUTTON TO GO TO EXTRAS SCENE, AND THE BACKGROUND MUSIC MAKES A BUZZING SOUND.
When at the Extras Scene after the completion of the game, the button becomes interactive, so I set it when I click on it, it pauses the background music (which is the buzzing in my case) and plays the audio clip. When the audio clip is done, the background music resumes (Buzzing-sound again). That one works well but the background music at that scene becomes unstable.
Screenshot of Extras scene BEFORE COMPLETION:
Screenshot of Extras scene (Where/When the buzzing occurs) AFTER COMPLETION:
Extras GameObject Manager Script: https://pastebin.com/RCEJ2Hzy
MusicPlayer script (placed on gameobject in the Menu scene): https://pastebin.com/gD429WgU
Methods implemented: I tried doing some adjustments in the ExtrasScript as the following:
public void PlaySound ()
{
if (didFinish == true && !audioSource.isPlaying)
{
themeSource.Pause ();
audioSource.PlayOneShot (sound1);
}
}
void Update ()
{
if (didFinish == true && !audioSource.isPlaying)
{
btnTxt.text = "Play Sound";
themeSource.Play ();
}
else if (didFinish == true && audioSource.isPlaying)
{
btnTxt.text = "Sound is Playing";
}
}
I apologize for any unclear points and long post, but this problem has been really weird and annoying. Any help would be great!
Answer by jackblue2009 · Oct 03, 2017 at 07:08 AM
After some time and a lot of headaches. I found a fix, (or at least a method that solved my problem).
Simply when the Audio clip is not playing in Extras scene, I set it to mute. I un-mute it only when I wanted to play the clip. Here is the correct adjustments of the code in ExtrasScript:
public void PlaySound ()
{
if (didFinish == true && !audioSource.isPlaying)
{
audioSource.mute = false; //Here I Un Mute it
themeSource.Pause ();
audioSource.PlayOneShot (sound1);
}
}
void Start ()
{
audioSource = GetComponent<AudioSource> ();
audioSource.mute = true; //Here I Mute it
themeSource = GameObject.Find ("Music Player").GetComponent<AudioSource> ();
if (didFinish == false)
{
soundBtn.interactable = false;
btnTxt.text = "Complete Game to UNLOCK";
}
else if (didFinish == true)
{
soundBtn.interactable = true;
btnTxt.text = "Play Sound";
audioSource.Stop ();
}
}
Answer by LukeWaffel · Oct 03, 2017 at 06:55 AM
It seems like a DontDestroyOnLoad problem. If there's an AudioSource in your main scene with a DontDestroyOnLoad tag, it'll stay when you switch to the game scene. However, when you get back to the main menu, there's yet another copy of it (Because you are reloading the scene with the audio source) so now you have two playing Audio Sources. You can fix this by only spawning the AudioSource once, either by checking a variable, or just checking whether or not your AudioSource already exists.
~LukeWaffel
I have already did that in the following script attached to the GameObject ($$anonymous$$usic Player) in the $$anonymous$$enu scene:
Oh, my apologies, I didn't know.
One little tip, I highly recommend to not call GetActiveScene in Update. I ins$$anonymous$$d recommend subscribing to the SceneLoaded callback of the Scene$$anonymous$$anager class as described here.
~LukeWaffel