/// <summary>Begins playing any audio</summary> private void playAudio() { // Get a full path (also uses {language} and {mood} and is relative to resources://Dialogue): // Localise it: string audioPath = audioFilePath.Replace("{language}", UI.Language); // Load the file now: AudioPackage req = new AudioPackage( audioPath, new Dom.Location("resources://Dialogue/", null) ); // Apply the audio package to the slides set: timingLeadBy(req); req.onload = delegate(UIEvent e){ // Play immediately - the system has been waiting for it // (if it's from resources then the delay would've been generally unnoticeable): req.Start(track.timeline.node); }; req.onerror = delegate(UIEvent e){ // Skip this frame: Dom.Log.Add("Audio file unavailable - PowerSlide will playback without it."); endTimingLead(); }; // Send it off: req.send(); }
public AudioSource PlayAudioPackage(AudioPackage thePackage, Vector3 position, float volParam = 1f, float delay = 0f, float pan = 0f) { AudioSource theSource; if (thePackage.theSource != null) { theSource = thePackage.theSource; } else { theSource = CreateNewSource (thePackage); } if (thePackage.voiceCount < thePackage.voiceMax) { theSource.clip = thePackage.theClip; theSource.volume = ((thePackage.baseVolume * volParam) / Random.Range (1f, 1f + thePackage.volRandom)); theSource.pitch = Random.Range (thePackage.basePitch - thePackage.pitchRandom / 2f, thePackage.basePitch + thePackage.pitchRandom / 2f); theSource.spatialBlend = thePackage.spatialBlend; theSource.panStereo = pan; theSource.transform.position = position; theSource.PlayDelayed (delay); ++thePackage.voiceCount; // Debug.Log(thePackage.ToString() + " playing at voiceCount: " + thePackage.voiceCount); StartCoroutine(FreeVoice(thePackage)); // float playTime = (thePackage.theClip.length / theSource.pitch) + delay; // Debug.Log("Audio package voiced for playTime (s): " + playTime); // theSource.gameObject.GetComponent<ActiveAudioSource> ().Invoke ("ReturnSourceToQueue", playTime); // StartCoroutine (NullPackage (thePackage, playTime)); } return theSource; }
// "Frees up" a voice within the given package, allowing the audio to be played again. IEnumerator FreeVoice(AudioPackage thePackage) { yield return new WaitForSeconds(thePackage.voiceDuration); if (thePackage.voiceCount > 0) { --thePackage.voiceCount; } yield return null; }
AudioSource CreateNewSource(AudioPackage thePackage) { string theName = (thePackage.theClip != null ? thePackage.theClip.name : "AudioSource"); GameObject newSource = new GameObject(theName); newSource.transform.parent = theTransform; newSource.transform.localPosition = Vector3.zero; AudioSource theAudioSource = newSource.AddComponent<AudioSource>(); theAudioSource.outputAudioMixerGroup = theGroup; theAudioSource.playOnAwake = false; theAudioSource.spread = 90f; // theAudioSource.spread = MasterSpread; // theAudioSource.SetCustomCurve(AudioSourceCurveType.Spread, spreadCurve); theAudioSource.rolloffMode = AudioRolloffMode.Logarithmic; // theAudioSource.minDistance = MasterDistanceMin; // theAudioSource.maxDistance = MasterDistanceMax; thePackage.theSource = theAudioSource; return theAudioSource; }