Exemplo n.º 1
0
        /// <summary>
        /// 播放音频
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private AudioItem Play(AudioItem item)
        {
            // 存入id字典
            if (item.id != null)
            {
                if (_idDict.ContainsKey(item.id))
                {
                    Stop(item);
                }
                _idDict[item.id] = item;
            }

            // 存入拥有者字典
            if (item.owner != null)
            {
                Dictionary <string, AudioItem> dict;
                if (_ownerDict.TryGetValue(item.owner, out dict) == false)
                {
                    dict = _ownerDict[item.owner] = new Dictionary <string, AudioItem>();
                }
                dict[item.player.url] = item;
            }

            item.player.completeCallback = (player) =>
            {
                if (item.callback != null)
                {
                    item.callback.Invoke(item);
                }
                ReleaseAudioItem(item);
            };
            item.player.Play();

            return(item);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 释放音频
        /// </summary>
        /// <param name="item"></param>
        private void ReleaseAudioItem(AudioItem item)
        {
            // 移除id字典
            if (item.id != null)
            {
                if (_idDict.TryGetValue(item.id, out item))
                {
                    _idDict.Remove(item.id);
                }
            }

            // 移除拥有者字典
            if (item.owner != null)
            {
                Dictionary <string, AudioItem> dict;
                if (_ownerDict.TryGetValue(item.owner, out dict))
                {
                    if (dict.ContainsKey(item.player.url))
                    {
                        dict.Remove(item.player.url);
                    }
                }
            }

            App.objectPoolManager.ReleaseObject(item.player);
            item.player   = null;
            item.callback = null;
        }
Exemplo n.º 3
0
 /// <summary>
 /// 设置音乐音量
 /// </summary>
 /// <param name="volume"></param>
 public void SetMusicVolume(float volume)
 {
     foreach (var v in _idDict)
     {
         AudioItem item = v.Value;
         if (item.type == AudioItemType.MUSIC)
         {
             item.player.volume = volume;
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 设置音效音量
 /// </summary>
 /// <param name="volume"></param>
 public void SetEffectVolume(float volume)
 {
     foreach (var v in _idDict)
     {
         AudioItem item = v.Value;
         if (item.type == AudioItemType.EFFECT)
         {
             item.player.volume = volume;
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public AudioItem PlayMusic(string url)
        {
            if (_musicEnabled == false)
            {
                return(null);
            }

            AudioItem item = CreateAudioItem(AudioItemType.MUSIC, url, url, null, _musicVolume, true, true);

            return(Play(item));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="url"></param>
        /// <param name="owner"></param>
        /// <param name="volume"></param>
        /// <param name="playWhenLoaded"></param>
        /// <returns></returns>
        public AudioItem PlayEffect(string url, string owner, float volume, bool playWhenLoaded)
        {
            if (_effectEnabled == false)
            {
                return(null);
            }

            AudioItem item = CreateAudioItem(AudioItemType.EFFECT, url, null, owner, _effectVolume * volume, false, playWhenLoaded);

            return(Play(item));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public AudioItem PlayEffect(string url)
        {
            if (_effectEnabled == false)
            {
                return(null);
            }

            AudioItem item = CreateAudioItem(AudioItemType.EFFECT, url, null, null, _effectVolume);

            return(Play(item));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="url"></param>
        /// <param name="id"></param>
        /// <param name="loop"></param>
        /// <param name="volume"></param>
        /// <param name="playWhenLoaded"></param>
        /// <param name="forcePlay"></param>
        /// <returns></returns>
        public AudioItem PlayMusic(string url, string id, bool loop, float volume, bool playWhenLoaded, bool forcePlay = false)
        {
            if (forcePlay == false && _idDict.ContainsKey(id))
            {
                return(_idDict[id]);
            }

            if (_musicEnabled == false)
            {
                return(null);
            }

            AudioItem item = CreateAudioItem(AudioItemType.MUSIC, url, id, null, _musicVolume * volume, loop, playWhenLoaded);

            return(Play(item));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="url"></param>
        /// <param name="id"></param>
        /// <param name="owner"></param>
        /// <param name="volume"></param>
        /// <param name="playWhenLoaded"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public AudioItem PlayEffect(string url, string id, string owner, float volume, bool playWhenLoaded, Action <AudioItem> callback)
        {
            if (_effectEnabled == false)
            {
                return(null);
            }

            // 判断是否已经正在播放
            if (owner != null && HasAudiosByOwner(url, owner))
            {
                return(null);
            }

            AudioItem item = CreateAudioItem(AudioItemType.EFFECT, url, id, owner, _effectVolume * volume, false, playWhenLoaded, callback);

            return(Play(item));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 创建音频
        /// </summary>
        /// <param name="type"></param>
        /// <param name="url"></param>
        /// <param name="id"></param>
        /// <param name="owner"></param>
        /// <param name="volume"></param>
        /// <param name="loop"></param>
        /// <param name="playWhenLoaded"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        private AudioItem CreateAudioItem(AudioItemType type, string url, string id = null, string owner = null,
                                          float volume = 1f, bool loop = false, bool playWhenLoaded = false, Action <AudioItem> callback = null)
        {
            AudioItem item = new AudioItem();

            item.type     = type;
            item.id       = id;
            item.owner    = owner;
            item.callback = callback;
            AudioPlayer player = App.objectPoolManager.GetObject <AudioPlayer>();

            player.url            = url;
            player.volume         = volume;
            player.loop           = loop;
            player.playWhenLoaded = playWhenLoaded;
            player.audioSource    = behaviour.gameObject.AddComponent <AudioSource>();
            item.player           = player;
            return(item);
        }
Exemplo n.º 11
0
 /// <summary>
 /// 停止播放
 /// </summary>
 /// <param name="item"></param>
 public void Stop(AudioItem item)
 {
     ReleaseAudioItem(item);
 }