public void PlayScheduled (double time);


timeTiempo en segundos en la línea de tiempo absoluta a la que AudioSettings.dspTime hace referencia cuando el sonido debería comenzar a reproducirse.


Reproduce el clip en un tiempo especifico en la linea de tiempo absoluta que AudioSettings.dspTime lee de.

Esta es la forma preferida de coser AudioClips en los reproductores de música porque es independiente de la velocidad de frames y da al sistema de audio tiempo suficiente para preparar la reproducción del sonido para buscarlo en medios donde la apertura y el almacenamiento en búfer tardan mucho tiempo ) Sin causar picos repentinos de la CPU.

using UnityEngine;
using System.Collections;

// Basic demonstration of a music system that uses PlayScheduled to preload and sample-accurately // stitch two AudioClips in an alternating fashion. The code assumes that the music pieces are // each 16 bars (4 beats / bar) at a tempo of 140 beats per minute. // To make it stitch arbitrary clips just replace the line // nextEventTime += (60.0 / bpm) * numBeatsPerSegment // by // nextEventTime += clips[flip].length;

[RequireComponent(typeof(AudioSource))] public class ExampleClass : MonoBehaviour { public float bpm = 140.0f; public int numBeatsPerSegment = 16; public AudioClip[] clips = new AudioClip[2];

private double nextEventTime; private int flip = 0; private AudioSource[] audioSources = new AudioSource[2]; private bool running = false;

void Start() { for (int i = 0; i < 2; i++) { GameObject child = new GameObject("Player"); child.transform.parent = gameObject.transform; audioSources[i] = child.AddComponent<AudioSource>(); }

nextEventTime = AudioSettings.dspTime + 2.0f; running = true; }

void Update() { if (!running) { return; }

double time = AudioSettings.dspTime;

if (time + 1.0f > nextEventTime) { // We are now approx. 1 second before the time at which the sound should play, // so we will schedule it now in order for the system to have enough time // to prepare the playback at the specified time. This may involve opening // buffering a streamed file and should therefore take any worst-case delay into account. audioSources[flip].clip = clips[flip]; audioSources[flip].PlayScheduled(nextEventTime);

Debug.Log("Scheduled source " + flip + " to start at time " + nextEventTime);

// Place the next event 16 beats from here at a rate of 140 beats per minute nextEventTime += 60.0f / bpm * numBeatsPerSegment;

// Flip between two audio sources so that the loading process of one does not interfere with the one that's playing out flip = 1 - flip; } } }

El ejemplo en AudioSource.SetScheduledEndTime muestra cómo puede reproducir dos clips de audio sin saltos ni clics entre los clips. El enfoque es tener dos AudioSources con clips adjuntos, y en-colar cada clip utilizando su AudioSource.

See Also: SetScheduledStartTime.