UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
AudioSource.PlayScheduled
# [AudioSource](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.html).PlayScheduled public void **PlayScheduled** (double **time**); ## 参数 | time | 声音应该开始播放的时间,该时间为 AudioSettings.dspTime 引用的绝对时间轴上的时间(以秒为单位)。 | | ------ | ------------------------------------------------------------------------------------------------- | ## 描述 在 AudioSettings.dspTime 读取的绝对时间轴上的特定时间播放 [clip](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource-clip.html)。 这是在音乐播放器中拼接 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](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetScheduledEndTime.html) 处的示例演示了播放两个音频剪辑并且不必在剪辑切换时进行弹出或点击操作的方法:设置两个附加剪辑的 AudioSource,然后使用 AudioSource 对每个剪辑进行排队。 另请参阅:[SetScheduledStartTime](https://docs.unity3d.com/cn/2019.4/ScriptReference/AudioSource.SetScheduledStartTime.html)。
da
2022年5月24日 10:06
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码