返回介绍

AudioSource.PlayScheduled 定时播放

发布于 2019-12-18 15:37:24 字数 5689 浏览 1220 评论 0 收藏 0

JavaScript => public function PlayScheduled(time: double): void;
C# => public void PlayScheduled(double time);

Parameters 参数

timeTime in seconds on the absolute time-line that AudioSettings.dspTime refers to for when the sound should start playing.
当声音应开始播放时,引用AudioSettings.dspTime绝对时间线上的时间。

Description 描述

Plays the clip at a specific time on the absolute time-line that AudioSettings.dspTime reads from.

在指定的时间播放剪辑,时间是读取AudioSettings.dspTime绝对时间线。

This is the preferred way to stitch AudioClips in music players because it is independent of the frame rate and gives the audio system enough time to prepare the playback of the sound to fetch it from media where the opening and buffering takes a lot of time (streams) without causing sudden CPU spikes.

这是做音乐播放器的首先方法,因为它是独立的帧速率,并且给了音频系统足够的时间来准备播放的声音,要打开媒体,以及缓冲需要很多的时间(流),而不会引起突然的CPU峰值。

JavaScript:

// 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;
 
@script RequireComponent(AudioSource)
 
public var bpm = 140.0;
public var numBeatsPerSegment = 16;
public var clips = new AudioClip[2];
 
private var nextEventTime:double;
private var flip = 0;
private var audioSources = new AudioSource[2];
private var running = false;
 
function Start()
{
	for(var i = 0; i < 2; i++)
	{
		var child:GameObject = new GameObject("Player");
		child.transform.parent = gameObject.transform;
		audioSources[i] = child.AddComponent.<AudioSource>();
	}
	nextEventTime = AudioSettings.dspTime + 2.0;
	running = true;
}
 
function Update()
{
	if(!running)
		return;
	var time = AudioSettings.dspTime;
	if(time + 1.0 > 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.0 / 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;
	}
}

C#:

using UnityEngine;
using System.Collections;
 
[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() {
        int i = 0;
        while (i < 2) {
            GameObject child = new GameObject("Player");
            child.transform.parent = gameObject.transform;
            audioSources[i] = child.AddComponent<AudioSource>();
            i++;
        }
        nextEventTime = AudioSettings.dspTime + 2.0F;
        running = true;
    }
    void Update() {
        if (!running)
            return;
 
        double time = AudioSettings.dspTime;
        if (time + 1.0F > nextEventTime) {
            audioSources[flip].clip = clips[flip];
            audioSources[flip].PlayScheduled(nextEventTime);
            Debug.Log("Scheduled source " + flip + " to start at time " + nextEventTime);
            nextEventTime += 60.0F / bpm * numBeatsPerSegment;
            flip = 1 - flip;
        }
    }
}

he example at AudioSource.SetScheduledEndTime shows how you can play two audio clips without pops or clicks between the clips. The approach is to have two AudioSources with clips attached, and queue up each clip using its AudioSource.

See Also: SetScheduledStartTime.

AudioSource 音频源

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文