Example #1
0
        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="serialId">声音的序列编号</param>
        /// <param name="soundAsset">声音资源</param>
        /// <param name="playSoundParams">播放声音参数</param>
        /// <param name="errorCode">错误码</param>
        /// <returns>用于播放的声音代理</returns>
        public SoundAgent PlaySound(int serialId, object soundAsset, PlaySoundParams playSoundParams, out PlaySoundErrorCode?errorCode)
        {
            errorCode = null;
            SoundAgent candidateAgent = null;

            foreach (SoundAgent soundAgent in m_SoundAgents)
            {
                if (!soundAgent.IsPlaying)
                {
                    candidateAgent = soundAgent;
                    break;
                }

                //如果当前声音代理正在播放
                if (soundAgent.Priority < playSoundParams.Priority)
                {
                    if (candidateAgent == null || soundAgent.Priority < candidateAgent.Priority)
                    {
                        candidateAgent = soundAgent;
                    }
                }
                else if (!AvoidBeingReplacedBySamePriority && soundAgent.Priority == playSoundParams.Priority)
                {
                    if (candidateAgent == null || soundAgent.SetSoundAssetTime < candidateAgent.SetSoundAssetTime)
                    {
                        candidateAgent = soundAgent;
                    }
                }
            }

            if (candidateAgent == null)
            {
                errorCode = PlaySoundErrorCode.IgnoredDueToLowPriority;
                return(null);
            }

            if (!candidateAgent.SetSoundAsset(soundAsset))
            {
                errorCode = PlaySoundErrorCode.SetSoundAssetFailure;
                return(null);
            }

            candidateAgent.SerialId           = serialId;
            candidateAgent.Time               = playSoundParams.Time;
            candidateAgent.MuteInSoundGroup   = playSoundParams.MuteInSoundGroup;
            candidateAgent.Loop               = playSoundParams.Loop;
            candidateAgent.Priority           = playSoundParams.Priority;
            candidateAgent.VolumeInSoundGroup = playSoundParams.VolumeInSoundGroup;
            candidateAgent.Pitch              = playSoundParams.Pitch;
            candidateAgent.PanStereo          = playSoundParams.PanStereo;
            candidateAgent.SpatialBlend       = playSoundParams.SpatialBlend;
            candidateAgent.MaxDistance        = playSoundParams.MaxDistance;

            //播放声音
            candidateAgent.Play(playSoundParams.FadeInSeconds);

            return(candidateAgent);
        }
Example #2
0
        private void LoadSoundSuccessCallback(string soundAssetName, object soundAsset, float duration, object userData)
        {
            LoadSoundInfo loadSoundInfo = (LoadSoundInfo)userData;

            if (loadSoundInfo == null)
            {
                Debug.LogError("加载声音的信息为空");
                return;
            }

            m_SoundsBeingLoaded.Remove(loadSoundInfo.SerialId);

            //释放需要在加载后立即释放的声音
            if (m_SoundsToReleaseOnLoad.Contains(loadSoundInfo.SerialId))
            {
                Debug.Log("在加载成功后释放了声音:" + loadSoundInfo.SerialId.ToString());
                m_SoundsToReleaseOnLoad.Remove(loadSoundInfo.SerialId);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                return;
            }

            //播放声音
            PlaySoundErrorCode?errorCode  = null;
            SoundAgent         soundAgent = loadSoundInfo.SoundGroup.PlaySound(loadSoundInfo.SerialId, soundAsset, loadSoundInfo.PlaySoundParams, out errorCode);

            if (soundAgent != null)
            {
                //获取到声音代理辅助器,并设置绑定的实体或位置
                PlaySoundInfo        playSoundInfo    = (PlaySoundInfo)loadSoundInfo.UserData;
                SoundAgentHelperBase soundAgentHelper = soundAgent.Helper;
                if (playSoundInfo.BindingEntity != null)
                {
                    soundAgentHelper.SetBindingEntity(playSoundInfo.BindingEntity);
                }
                else
                {
                    soundAgentHelper.SetWorldPosition(playSoundInfo.WorldPosition);
                }

                //派发播放声音成功事件
                PlaySoundSuccessEventArgs se = ReferencePool.Acquire <PlaySoundSuccessEventArgs>();
                m_EventManager.Fire(this, se.Fill(loadSoundInfo.UserData, loadSoundInfo.SerialId, soundAssetName, soundAgent, duration));
            }
            else
            {
                m_SoundsToReleaseOnLoad.Remove(loadSoundInfo.SerialId);
                m_SoundHelper.ReleaseSoundAsset(soundAsset);
                string errorMessage = string.Format("声音组: {0} 播放声音 '{1}' 失败.", loadSoundInfo.SoundGroup.Name, soundAssetName);

                //派发播放声音失败事件
                PlaySoundFailureEventArgs fe = ReferencePool.Acquire <PlaySoundFailureEventArgs>();
                m_EventManager.Fire(this, fe.Fill(loadSoundInfo.UserData, loadSoundInfo.SerialId, soundAssetName, loadSoundInfo.SoundGroup.Name, loadSoundInfo.PlaySoundParams, errorCode.Value, errorMessage));
                Debug.LogError("播放声音失败:" + errorMessage);
            }
        }
Example #3
0
        /// <summary>
        /// 填充播放声音成功事件
        /// </summary>
        /// <returns>播放声音成功事件</returns>
        public PlaySoundSuccessEventArgs Fill(object userData, int serialId, string soundAssetName, SoundAgent soundAgent, float duration)
        {
            PlaySoundInfo playSoundInfo = (PlaySoundInfo)userData;

            SerialId       = serialId;
            SoundAssetName = soundAssetName;
            BindingEntity  = playSoundInfo.BindingEntity;
            UserData       = playSoundInfo.UserData;

            SoundAgent = soundAgent;
            Duration   = duration;

            return(this);
        }