Example #1
0
 // Stops playback of any audio source with the specified id.
 public void Stop(string id)
 {
     if (m_library.ContainsKey(id))
     {
         AudioFileHandler handler = m_library[id];
         if (handler != null)
         {
             if (!handler.m_isBgm)
             {
                 if (Application.platform == RuntimePlatform.Android && m_UseAndroidAudioBridgeForSfx)
                 {
                     AdnAudioBridge.StopSound(handler.m_AudioBridgeId);
                 }
                 else if (handler.m_sfx != null)
                 {
                     m_library[id].m_sfx.Stop();
                 }
             }
             else
             {
                 m_bgm.Stop(handler.AudioClipFile);
             }
         }
     }
 }
Example #2
0
    // Plays a prepared sfx of the specified id.
    public void PlaySfx(string id, float delay = 0.0f, bool loop = false, bool isSfxOverBgm = false)
    {
        if (m_library == null)
        {
            DebugUtil.LogError("Cannot play sfx without library!");
            return;
        }

        if (m_library.ContainsKey(id))
        {
            AudioFileHandler handler = m_library[id];
            if (handler == null || handler.m_isBgm)
            {
                DebugUtil.LogError("Handler null or is not sfx: " + handler.m_fileInfo);
                return;
            }

            if (!handler.m_isReadyForPlayback)
            {
                DebugUtil.LogWarning(
                    string.Format("PlaySfx: Cannot play {0} because it is not fully loaded yet. Try again later.",
                                  id));
            }

            bool isAdnBridgeSfx = Application.platform == RuntimePlatform.Android && m_UseAndroidAudioBridgeForSfx && !handler.m_isBgm;

            if (!isAdnBridgeSfx && handler.m_sfx == null)
            {
                DebugUtil.LogError("PlaySfx m_sfx of handler is null!");
                return;
            }

            if (isSfxOverBgm)
            {
                float clipLength         = handler.m_audioLength;
                float sfxOverBgmNewValue = Time.time + clipLength + delay;
                if (sfxOverBgmNewValue > m_SfxOverBGmVolumeEndTime)
                {
                    m_SfxOverBGmVolumeEndTime = sfxOverBgmNewValue;
                }
            }

            if (isAdnBridgeSfx)
            {
                handler.m_LastAudioBridgeStream = AdnAudioBridge.PlaySound(handler.m_AudioBridgeId, GetSfxVolumeFinal());
            }
            else
            {
                handler.m_sfx.Play(delay, loop);
            }
        }
        else
        {
            DebugUtil.LogWarning(string.Format("PlaySfx: No such id {0} exists in SoundManager.", id));
        }
    }
Example #3
0
    // Returns true if the specified id exists and is fully loaded (PrepareSound is completely finished).
    public bool IsReadyForPlayback(string id)
    {
        if (!m_library.ContainsKey(id))
        {
            DebugUtil.LogWarning(string.Format("IsReadyForPlayback: {0} doesn't exist.", id));
            return(false);
        }

        AudioFileHandler handler = m_library[id];

        return(handler.m_isReadyForPlayback);
    }
Example #4
0
    // Returns true if the specified audio clip of the specified id is playing.
    public bool IsPlaying(string id)
    {
        if (m_library.ContainsKey(id))
        {
            AudioFileHandler handler = m_library[id];
            if (handler != null)
            {
                if (!handler.m_isBgm && handler.m_sfx != null)
                {
                    return(m_library[id].m_sfx.IsPlaying());
                }
                else if (handler.m_isBgm)
                {
                    return(m_bgm.IsBgmPlaying(handler.AudioClipFile));
                }
            }
        }

        return(false);
    }
Example #5
0
 // Plays a single bgm of the specified id.
 public void PlayBgm(string id, bool loop = true, float crossfadeTime = 0.0f)
 {
     if (m_library.ContainsKey(id))
     {
         AudioFileHandler handler = m_library[id];
         if (handler.m_isReadyForPlayback)
         {
             m_bgm.Play(handler.AudioClipFile, loop, crossfadeTime);
         }
         else
         {
             DebugUtil.LogWarning(
                 string.Format("PlayBgm: Cannot play {0} because it is not fully loaded yet. Try again later.",
                               id));
         }
     }
     else
     {
         DebugUtil.LogWarning(string.Format("PlayBgm: No such id {0} exists in SoundManager.", id));
     }
 }
Example #6
0
    // Prepares/loads the desired sound in memory. The id is the string name used to identify their corresponding
    // audio files. IsBgm is wheter the files are to be used as bgm. is3D and stream are flags to identify
    // how the audio files will be used.
    public void PrepareSound(string id, string file, bool isBgm, bool is3D, bool stream)
    {
        if (m_library.ContainsKey(id))
        {
            DebugUtil.LogWarning(string.Format("PrepareFile {0} is already loaded in the library. Increasing reference count instead.", id));
            m_library[id].m_refCount++;
            return;
        }

        string soundFile = GetSoundFileForPlatform(file);

        if (Application.platform != RuntimePlatform.Android && !File.Exists(soundFile))
        {
            DebugUtil.LogError(string.Format("File {0} is illegal or non-existent.", soundFile));
        }

        AudioFileHandler handler = new AudioFileHandler(soundFile, isBgm, m_SfxBufferSize);

        m_library.Add(id, handler);
        StartCoroutine(LoadAudioClipFromFile(handler, is3D, stream));
    }
Example #7
0
 // The unprepare sound is to unload from memory the audio file that corresponds to given id(s).
 public void UnprepareSound(string id)
 {
     if (m_library.ContainsKey(id))
     {
         AudioFileHandler handler = m_library[id];
         if (handler.m_refCount <= 1)
         {
             Stop(id);
             if (Application.platform == RuntimePlatform.Android && m_UseAndroidAudioBridgeForSfx && !handler.m_isBgm)
             {
                 AdnAudioBridge.UnloadSound(handler.m_AudioBridgeId);
             }
             m_library.Remove(id);
         }
         else
         {
             m_library[id].m_refCount--;
         }
     }
     else
     {
         DebugUtil.LogWarning(string.Format("UnprepareSound {0} doesn't exist to unload.", id));
     }
 }
Example #8
0
    private IEnumerator LoadAudioClipFromFile(AudioFileHandler handler, bool is3D, bool stream)
    {
        string[] parts = handler.m_fileInfo.Split('\\');

        string path    = "";
        string adnPath = "";

        if (Application.platform == RuntimePlatform.Android)
        {
            path    = handler.m_fileInfo;
            adnPath = handler.m_fileInfo;
            if (m_UseAndroidAudioBridgeForSfx && !handler.m_isBgm)
            {
                // Note: For AdnAudioBridge, we only need the path relative to the StreamingAssets folder.

                // remove streaming path at the beginning
                if (adnPath.Contains(AppDir.StreamPath))
                {
                    adnPath = adnPath.Remove(0, AppDir.StreamPath.Length);
                }

                // remove / or \ at the beginning if any.
                if (adnPath.StartsWith("/") || adnPath.StartsWith("\\"))
                {
                    adnPath = adnPath.Remove(0, 1);
                }
            }
        }
        else
        {
            path = "file://" + handler.m_fileInfo;
        }

        if (Application.platform == RuntimePlatform.Android && m_UseAndroidAudioBridgeForSfx && !handler.m_isBgm)
        {
            handler.m_AudioBridgeId = AdnAudioBridge.LoadSound(adnPath);
        }

        // Note: We still load audio clip even if we use AdnAudioBridge because we need to get the length of the sound.
        // We will unload it later once we receive this information.
        WWW www = new WWW(path);

        while (www.progress < 1 || !www.audioClip.isReadyToPlay)
        {
            yield return(www);
        }

        AudioClip clip = www.GetAudioClip(is3D, stream);

        if (clip == null)
        {
            DebugUtil.LogError(string.Format("Failed to load AudioClip: {0}", path));
        }
        else
        {
            DebugUtil.Log(string.Format("Successfully loaded AudioClip: {0}", path));
            clip.name                    = parts[parts.Length - 1];
            handler.AudioClipFile        = clip;
            handler.m_audioLength        = clip.length;
            handler.m_isReadyForPlayback = true;

            if (!handler.m_isBgm)
            {
                if (Application.platform == RuntimePlatform.Android && m_UseAndroidAudioBridgeForSfx)
                {
                    // NOTE: We unload the audio clip file from memory because we won't need it when using AdnAudioBridge
                    handler.AudioClipFile = null;
                }
                else
                {
                    handler.PrepareSfxBuffers(GetSfxVolumeFinal());
                }
            }
        }
    }