/// <summary> /// Processes a queued sound packet. /// </summary> /// <param name="packet"></param> private void ProcessSoundPacket(SoundPacket packet) { //------------------------------------------------------------------ // Cache off the settings to use before we start playing sounds. //------------------------------------------------------------------ //int count = (int)e.Argument; PlaySoundType soundType = packet.Settings.PlaySoundType; // this.settings.PlaySoundType; string soundDirectory = packet.Settings.SoundDirectory; // this.settings.SoundDirectory; string soundFile = packet.Settings.SoundFile; // this.settings.SoundFile; this.SetSoundVolume(packet.Settings); if (soundType == PlaySoundType.FromDirectory) { if (!string.IsNullOrEmpty(soundDirectory) && Directory.Exists(soundDirectory)) { try { for (int i = 0; i < packet.Count; i++) { List <string> fileList = new List <string>(); foreach (string file in Directory.EnumerateFiles(soundDirectory, "*.*")) { FileInfo info = new FileInfo(file); switch (info.Extension) { case ".wav": fileList.Add(file); break; } } Random random = new Random(); this.PlaySynchronousSound(fileList[random.Next(fileList.Count - 1)]); } } catch (Exception) { /*Ignore any exceptions and continue.*/ } } } else if (soundType == PlaySoundType.FromFile) { try { if (File.Exists(soundFile)) { for (int i = 0; i < packet.Count; i++) { this.PlaySynchronousSound(soundFile); } } } catch (Exception) { /*Ignore any exceptions.*/ } } }
/// <summary> /// Plays a test sound for a specific file or from a directory of sound files. /// </summary> /// <param name="soundType"></param> /// <param name="path"></param> private void PlayTestSound(PlaySoundType soundType, string path) { SoundPlaySettings tempSettings = new SoundPlaySettings(); tempSettings.PlaySoundType = soundType; switch (soundType) { case PlaySoundType.FromDirectory: tempSettings.SoundDirectory = path; break; case PlaySoundType.FromFile: tempSettings.SoundFile = path; break; default: return; } tempSettings.SoundVolume = this.uiVolumeTrackBar.Value; this.ParentView.Engine.SoundPlayer.ScheduleSound(1, tempSettings); }
public void PlaySoundEffect(AudioClip audio, PlaySoundType playSoundType = PlaySoundType.Override) { if (soundEffect == null || !SoundEffectEnable) { return; } if (soundEffect.isPlaying) { if (playSoundType == PlaySoundType.Override) { soundEffect.Stop(); } else if (playSoundType == PlaySoundType.Duplicate) { var dup = soundEffect.Spawn().GetComponent <AudioSource>(); if (dup) { dup.PlayOneShot(audio, SoundEffectVolume); } return; } } soundEffect.PlayOneShot(audio, SoundEffectVolume); }