Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stop sound not working

Discussion in 'Scripting' started by Shayke, Aug 21, 2018.

  1. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Hi, i always use the same codes and this time the sound wont stop.
    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.  
    8.     public Sound[] sounds;
    9.  
    10.     public static AudioManager instance;
    11.  
    12.     void Awake()
    13.     {
    14.      
    15.         if (instance == null) { instance = this; } else { Destroy(gameObject); return; }
    16.         DontDestroyOnLoad(gameObject);
    17.  
    18.         // DontDestroyOnLoad(this);
    19.  
    20.         // if (FindObjectsOfType(GetType()).Length > 1)
    21.         // {
    22.         //    Destroy(gameObject);
    23.         // }
    24.  
    25.         foreach (Sound s in sounds)
    26.         {
    27.             s.source = gameObject.AddComponent<AudioSource>();
    28.             s.source.clip = s.clip;
    29.  
    30.             s.source.volume = s.volume;
    31.             s.source.pitch = s.pitch;
    32.             s.source.loop = s.loop;
    33.         }
    34.     }
    35.     void Start()
    36.     {
    37.         {
    38.            // Play("BGM");
    39.         }
    40.     }
    41.     public void Play(string name)
    42.     {
    43.         Sound s = Array.Find(sounds, sounds => sounds.name == name);
    44.         if (s == null)
    45.             return;
    46.         s.source.Play();
    47.     }
    48.     public void Stop(string name)
    49.     {
    50.         Sound s = Array.Find(sounds, sounds => sounds.name == name);
    51.         if (s == null)
    52.             return;
    53.         s.source.Stop();
    54.     }
    55.  
    56.     public void updateSound()
    57.     {
    58.         foreach (Sound s in sounds)
    59.         {
    60.             s.source = gameObject.AddComponent<AudioSource>();
    61.             s.source.clip = s.clip;
    62.  
    63.             s.source.volume = s.volume;
    64.             s.source.pitch = s.pitch;
    65.             s.source.loop = s.loop;
    66.         }
    67.     }
    68. }
    69.  
    I am trying to stop the "BGM" by Stop("BGM"); and somehow it does not work.
    if i use the Stop function right after the Play function, it works. Weird.
    Anyone has a guess?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You seem to be adding multiple audio sources. Is that really your intent? I am guessing that you are stopping the audio on another source leaving the original source still playing.
     
  3. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I guess you are right but i have no idea how to do it in another way.
    I feel like i have some memory issues because of that too.
    What can i do?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In
    Awake()
    just call
    GetComponent<AudioSource>();
    and store the return value in a member variable. Reference the audio source from that variable instead.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    my guess is you're adding multiple audio sources on the same object(and blocking you GetComponent from getting the right one)

    mind showing us the Sound stucture and the calls to this methods?
     
  6. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I tryed but i don't really understand where to do it.
    When i hit play i am basiaclly multiplying the sound??

    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using UnityEngine;
    3.  
    4. [System.Serializable]
    5. public class Sound
    6. {
    7.     public string name;
    8.     public bool loop;
    9.     public AudioClip clip;
    10.  
    11.     [Range(0f, 1f)]
    12.     public float volume;
    13.     [Range(0.1f, 3f)]
    14.     public float pitch;
    15.  
    16.     [HideInInspector]
    17.     public AudioSource source;
    18.  
    19. }
    20.  
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to clarify - what exactly are you wanting to achieve with this
    AudioManager
    class? For example, is this just for ambience (e.g. general background music) or for object sound effects (like a fire playing a constant crackly-fire audio)?

    Also, are you familiar with the Unity help page regarding AudioSource (playing, stopping, play one shot, play at point etc.)?
     
  8. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I did'nt read that page.
    With my audio manager i control everything include ambience.
    I never had any problems with that but now i am trying to stop the music and i ran into it.
    What i figured now is the fact that the sounds actually was the cause of bad performance.

    What basiaclly i want is a gameobject with all the sounds and i just need to pick the sound to play it by writing Play("Sound");

    It works fine now but as you said before, the sound is multiplying.
     
  9. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Ok guys i found the solution to my problem.
    Thanks :)

    Edit:
    No i did'nt find.
    I seperated the bgm from the sound effects.
    Bottom line, i know how to control the volume and shut the music when i want but the issue is the performance.

    This is how i work with one BGM
    But with an array its different, isnt it?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OneSound : MonoBehaviour {
    6.  
    7.     public AudioClip BGMClip;
    8.     public AudioSource TheSource;
    9.     public static OneSound instance;
    10.  
    11.     private void Awake()
    12.     {
    13.      
    14.         if (instance == null) { instance = this; } else { Destroy(gameObject); return; }
    15.         DontDestroyOnLoad(gameObject);
    16.  
    17.         AudioManager.CurrentVol = 1;
    18.  
    19.         TheSource.clip = BGMClip;
    20.        // TheSource.Play();
    21.     }
    22. }
    23.  
     
    Last edited: Aug 23, 2018
  10. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    What is the issue with the performance? It stops a some time after you issue the command- or it slows the frame rate?

    Yes, to the extent that an array of <T> is a collection and nothing to do with <T> itself. So, based on your
    OneSound
    , the handling could look something like this (note: this is untested) :
    Code (CSharp):
    1. public class BgMusicMgr : MonoBehaviour
    2. {
    3.     public static BgMusicMgr Instance { get { return m_Instance; } }
    4.  
    5.     public void Play(string name)
    6.     {
    7.         if(m_allClips.ContainsKey(name))
    8.         {
    9.             theSource.clip = m_allClips[name];
    10.             theSource.Play();
    11.         }
    12.     }
    13.  
    14.     private void Awake()
    15.     {
    16.         if (m_Instance == null)
    17.             m_Instance = this;
    18.         else
    19.         {
    20.             Destroy(gameObject);
    21.             return;
    22.         }
    23.  
    24.         DontDestroyOnLoad(gameObject);
    25.         theSource.volume = 1;
    26.  
    27.         for (int i = 0; i < allMusic.Length; i++)
    28.             m_allClips[allMusic[i].name] = allMusic[i];
    29.     }
    30.  
    31.     static BgMusicMgr m_Instance;
    32.  
    33.     [SerializeField] AudioClip[] allMusic;
    34.     [SerializeField] AudioSource theSource;
    35.     Dictionary<string, AudioClip> m_allClips = new Dictionary<string, AudioClip>();
    36. }