Example #1
0
        /// <summary>/// 停止播放一个音效/// </summary>
        /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param>
        public void StopNormalAudio(string audioPath, bool isReleaseClip = false)
        {
            if (string.IsNullOrEmpty(audioPath))
            {
                return;
            }
            string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath);

            if (string.IsNullOrEmpty(audioName))
            {
                return;
            }

            var isPlaying = IsPlayingNormalAudio(audioName, out var curAudioSources);

            if (isPlaying == false)
            {
                return;
            }
            curAudioSources.Stop();
            if (isReleaseClip)
            {
                AudioClip clip = curAudioSources.clip;
                curAudioSources.clip = null;
                ResourcesManagerUtility.ReleaseComponentReferenceAsset(curAudioSources, clip);
            }

            RecycleNormalAudioSource(curAudioSources);
        }
Example #2
0
        /// <summary>/// 普通的音效播放/// </summary>
        /// <param name="isforceReplay">如果正在播放是否强制重播</param>
        public bool PlayNormalAudioSync(string audioPath, float volume, bool isforceReplay = false)
        {
            if (string.IsNullOrEmpty(audioPath))
            {
                return(false);
            }
            string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath);

            if (string.IsNullOrEmpty(audioName))
            {
                return(false);
            }

            if (IsPlayingNormalAudio(audioName, out var curAudioSource))
            {
                if (isforceReplay)
                {
                    curAudioSource.Stop();
                    curAudioSource.Play();
                }
                return(true);
            }

            curAudioSource = GetNextAvailableAudioSource();
            LoadAssetResult <AudioClip> assetInfor = ResourcesManagerUtility.LoadAssetSync <AudioClip>(audioPath, null, null);

            if (assetInfor == null)
            {
                mAllAvailableAudioSources.Push(curAudioSource);
                return(false);
            }
            else
            {
                assetInfor.ReferenceWithComponent(curAudioSource, (audioClip) =>
                {
                    if (curAudioSource.clip == audioClip)
                    {
                        return;
                    }
                    ResourcesManagerUtility.ReleaseComponentReferenceAsset(curAudioSource, curAudioSource.clip);
                    curAudioSource.clip = audioClip;
                });
                curAudioSource.volume = volume;
                mAllPlayingAudioSources.AddLast(curAudioSource);
                return(true);
            }
        }
Example #3
0
        private bool PlayBackgroundAudioSync(LoadAssetResult <AudioClip> clipAsset, float volume, bool isLoop = true, bool isforceReplay = false)
        {
            if (clipAsset == null)
            {
                return(false);
            }

            if (mCurBackgroundAudioSource.clip != null)
            {
                if (mCurBackgroundAudioSource.clip.name == clipAsset.mLoadAssetInstanceName)
                {
                    mCurBackgroundAudioSource.volume = volume;
                    mCurBackgroundAudioSource.loop   = isLoop;
                    if (isforceReplay)
                    {
                        mCurBackgroundAudioSource.Stop();
                        mCurBackgroundAudioSource.Play();
                    }

                    return(true);
                } //当前正在播放这个背景音
                else
                {
                    mCurBackgroundAudioSource.Stop(); //结束当前的背景音
                }
            }

            clipAsset.ReferenceWithComponent(mCurBackgroundAudioSource, (audioClip) =>
            {
                if (mCurBackgroundAudioSource.clip == audioClip)
                {
                    return;
                }
                ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, mCurBackgroundAudioSource.clip);
                mCurBackgroundAudioSource.clip = audioClip;
                return;
            });

            mCurBackgroundAudioSource.volume = volume;
            mCurBackgroundAudioSource.loop   = isLoop;
            mCurBackgroundAudioSource.Play();

            return(true);
        }
Example #4
0
        /// <summary>///  停止当前背景/// </summary>
        /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param>
        public void StopBackgroundAudio(bool isReleaseClip = false)
        {
            if (mCurBackgroundAudioSource == null)
            {
                return;
            }
            if (null == mCurBackgroundAudioSource.clip)
            {
                return;
            }

            mCurBackgroundAudioSource.Stop();
            if (isReleaseClip)
            {
                AudioClip clip = mCurBackgroundAudioSource.clip;
                mCurBackgroundAudioSource.clip = null;
                ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, clip);
            }
        }
Example #5
0
        /// <summary>/// 停止播放背景/// </summary>
        /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param>
        private void StopBackgroundAudio(string audioName, bool isReleaseClip = false)
        {
            if (mCurBackgroundAudioSource == null)
            {
                return;
            }
            if (mCurBackgroundAudioSource.clip == null)
            {
                return;
            }
            if (mCurBackgroundAudioSource.clip.name != audioName)
            {
                return;
            }

            mCurBackgroundAudioSource.Stop();
            if (isReleaseClip)
            {
                AudioClip clip = mCurBackgroundAudioSource.clip;
                mCurBackgroundAudioSource.clip = null;
                ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, clip);
            }
        }