time | 声音应该开始播放的时间,该时间为 AudioSettings.dspTime 引用的绝对时间轴上的时间(以秒为单位)。 |
在 AudioSettings.dspTime 读取的绝对时间轴上的特定时间播放 clip。
这是在音乐播放器中拼接 AudioClip 的首选方法,因为它与帧率无关,并且为音频系统提供了足够的时间来准备声音的播放(从打开和缓冲需要大量时间(流式传输)的媒体获取声音),而不导致突发的 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; } } }
AudioSource.SetScheduledEndTime 处的示例演示了播放两个音频剪辑并且不必在剪辑切换时进行弹出或点击操作的方法:设置两个附加剪辑的 AudioSource,然后使用 AudioSource 对每个剪辑进行排队。
另请参阅:SetScheduledStartTime。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.