Exemple #1
0
    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="abName">音效的ab名字</param>
    /// <param name="loop">是否循环</param>
    /// <param name="isFade">是否淡化背景音乐</param>
    /// <returns> 返回音效id</returns>
    public void PlaySound(int id, bool loop = false, Action action = null, bool isFade = true)
    {
        if (CurrentSoundId >= 39 && CurrentSoundId <= 45)
        {
            return;
        }
        StopSound();
        soundID++;
        var audioSource = gameObject.AddComponent <AudioSource>();

        Debug.Log(GetDubbingName(id)[0] + "----" + GetDubbingName(id)[1]);
        AudioClip audioClip = ResourceManager.GetInstance.LoadAudioClip(GetDubbingName(id)[0], GetDubbingName(id)[1]);

        //动态添加音效
        audioSource.clip   = audioClip;
        audioSource.loop   = loop;
        audioSource.volume = volume;
        audioSource.Play();
        CurrentSoundId = id;
        SoundDictionary.Add(soundID, audioSource);
        if (isFade)
        {
            foreach (var item in SoundDictionary)
            {
                if (item.Key != soundID)
                {
                    item.Value.DOFade(0.2f, 1f);
                }
            }
            CurrentMusic.DOFade(0.2f, 2);
        }
        StartCoroutine(PlaySoundEndDestroy(audioSource, soundID, action, isFade));
    }
        public void PlayMusic(string musicType, string name)
        {
            if (MusicDictionary.ContainsKey(musicType))
            {
                if (MusicDictionary[musicType].ContainsKey(name))
                {
                    string newMusicLocation = MusicDictionary[musicType][name];

                    if (newMusicLocation != CurrentMusicLocation)
                    {
                        CurrentMusicLocation = newMusicLocation;
                        if (CurrentMusic != null)
                        {
                            CurrentMusic.Dispose();
                        }
                        CurrentMusic = new Music(CurrentMusicLocation)
                        {
                            Loop   = true,
                            Volume = 10
                        };
                        CurrentMusic.Play();
                    }
                }
                else
                {
                    throw new ArgumentException("There is no music of this name", "musicType");
                }
            }
            else
            {
                throw new ArgumentException("There is no music of this type", "musicType");
            }
        }
Exemple #3
0
        internal override void ProcessMusicError(SoundMusicEventNotification eventNotification)
        {
            if (eventNotification.Event != SoundMusicEvent.ErrorOccurred) // no errors
            {
                return;
            }

            var soundMusicName = "Unknown";

            if (CurrentMusic != null)
            {
                CurrentMusic.SetStateToStopped();
                soundMusicName = CurrentMusic.Name;
            }

            Logger.Error("Error while playing the sound music '{0}'. Details follows:");

            var errorEvent = (MediaPlayer.ErrorEventArgs)eventNotification.EventData;

            if (errorEvent.What == MediaError.Unknown && errorEvent.Extra == 0xfffffc0e) // MEDIA_ERROR_UNSUPPORTED (Hardware dependent?)
            {
                Logger.Error("The data format of the music file is not supported.");
            }
            else if ((uint)errorEvent.What == 0xffffffed) // underlying audio track returned -12 (no memory) -> try to recreate the player once more
            {
                Logger.Error("The OS did not have enough memory to create the audio track.", soundMusicName);
            }
            else
            {
                Logger.Error(" [Details: ErrorCode={1}, Extra={2}]", soundMusicName, errorEvent.What, errorEvent.Extra);
            }

            // reset the music player to a valid state for future plays
            ResetMusicPlayer();
        }
 public void StopMusic()
 {
     if (CurrentMusic != null)
     {
         CurrentMusic.Dispose();
     }
     CurrentMusic         = null;
     CurrentMusicLocation = null;
     CurrentMusicType     = null;
 }
Exemple #5
0
        public void Play(string musicName, bool loopTrack = true)
        {
            if (IsPlaying)
            {
                Stop();
            }

            CurrentMusicName = Exceptions(musicName);
            CurrentMusic?.Play(Volume, loopTrack);
        }
Exemple #6
0
        public void PushDynamicMusic(DynamicMusic music, bool restart = false)
        {
            if (!restart && CurrentMusic != null)
            {
                if (CurrentMusic.Equals(music))
                {
                    return;
                }
            }

            PlayDynamicMusic(music);
        }
Exemple #7
0
        public void PushDynamicMusic(DynamicMusic music, bool restart, float crossFadeTime)
        {
            if (!restart && CurrentMusic != null)
            {
                if (CurrentMusic.Equals(music))
                {
                    return;
                }
            }

            PlayDynamicMusic(music);
        }
    public void PlayMusic(AudioClip music, float volume)
    {
        if (_music?.Music == music)
        {
            return;
        }
        if (_music != null)
        {
            _music.FadeAndDestroy();
        }

        _music = new CurrentMusic(_prefab, music, _fadeTime, volume, _curve);
        _music.FadeIn();
    }
Exemple #9
0
 public void PlayCrossFade(string musicName, TimeSpan duration, bool loopTrack = true)
 {
     if (!IsPlaying)
     {
         Play(musicName, loopTrack);
     }
     else
     {
         CurrentMusic.FadeOut(duration);
         CurrentMusicName = musicName;
         CurrentMusic.Stop();
         CurrentMusic.PlayFadeIn(Volume, duration, loopTrack);
     }
 }
Exemple #10
0
        internal void PrepareMediaPlayer()
        {
            if (CurrentMusic == null)
            {
                CurrentMusic = this;
            }
            else if (CurrentMusic != this)
            {
                CurrentMusic.Pause();
                CurrentMusic = this;
            }

            MediaPlayer.IsRepeating = IsLooped;
            MediaPlayer.Volume      = Volume * MasterVolume;
        }
Exemple #11
0
 protected override void Update()
 {
     base.Update();
     if (musicList.Count > 1)
     {
         //需要维护队列
         if (CurrentMusic && !CurrentMusic.isPlaying)
         {
             CurrentMusic.Stop();
             musicList.Remove(CurrentMusic);
             Destroy(CurrentMusic);
             CurrentMusic = musicList[0];
             CurrentMusic.Play();
         }
     }
 }
Exemple #12
0
        internal override void StartMusic()
        {
            if (audioPlayer == null)
            {
                return;
            }

            if (!audioPlayer.Play())
            {
                // this happens sometimes when we put the application on background when starting to play.
                var currentMusicName = CurrentMusic.Name;
                CurrentMusic.SetStateToStopped();
                ResetMusicPlayer();

                Logger.Warning("The music '{0}' failed to start playing.", currentMusicName);
            }
        }
Exemple #13
0
 /// <summary>
 /// 播放背景音乐
 /// </summary>
 /// <param name="abName">名字</param>
 /// <param name="loop">是否循环</param>
 /// <param name="isTrack">是否音轨</param>
 public void PlayMusic(string abName, string assetname, bool loop = true, bool isTrack = false)
 {
     if (!isTrack)
     {
         if (CurrentMusic && CurrentMusic.clip.name == assetname)
         {
             CurrentMusic.time = 0;
             CurrentMusic.Play();
         }
         else
         {
             if (musicList.Count != 0)
             {
                 foreach (var source in musicList)
                 {
                     if (source.clip.name == assetname)
                     {
                         CurrentMusic = source;
                         CurrentMusic.Play();
                     }
                     else
                     {
                         source.Stop();
                     }
                 }
             }
         }
     }
     else
     {
         foreach (var source in musicList)
         {
             if (source.clip.name == assetname)
             {
                 source.Play();
             }
             else
             {
                 source.Stop();
             }
         }
     }
 }
Exemple #14
0
    /// <summary>
    /// 音乐自动销毁
    /// </summary>
    /// <param name="audioclip"></param>
    /// <param name="soundID"></param>
    /// <returns></returns>
    private IEnumerator PlaySoundEndDestroy(AudioSource audioclip, int soundID, Action action, bool isFade)
    {
        yield return(new WaitForSeconds(audioclip.clip.length + 1));

        StopSound(soundID);
        if (isFade)
        {
            foreach (var item in SoundDictionary)
            {
                item.Value.DOFade(volume, 0.2f);
            }
            CurrentMusic.DOFade(volume, 0.2f);
        }

        if (action != null)
        {
            action();
        }
    }
 public void PlayRandomMusic(string musicType)
 {
     if (MusicDictionary.ContainsKey(musicType))
     {
         if (musicType != CurrentMusicType)
         {
             CurrentMusicType = musicType;
             if (CurrentMusic != null)
             {
                 CurrentMusic.Dispose();
             }
             CurrentMusic = new Music(MusicDictionary[musicType].ElementAt(new Random().Next(0, MusicDictionary[musicType].Count)).Value)
             {
                 Loop   = true,
                 Volume = 10
             };
             CurrentMusic.Play();
         }
     }
     else
     {
         throw new ArgumentException("There is no music of this type", "musicType");
     }
 }
Exemple #16
0
 public void Pause(TimeSpan duration) => CurrentMusic?.Pause(duration);
Exemple #17
0
 public void FadeOut(TimeSpan duration) => CurrentMusic?.FadeOut(duration);
Exemple #18
0
 public void Resume() => CurrentMusic?.Resume();
Exemple #19
0
 public void Pause() => CurrentMusic?.Pause();
Exemple #20
0
 public void Stop() => CurrentMusic?.Stop();
Exemple #21
0
 public void Update(GameTime gameTime) => CurrentMusic?.Update(gameTime);