我正在尝试统一编程一个准确的节拍器。我知道,已经有一些问题,但我找不到我的问题的解决方案。我的第一个幼稚实现使用了 Play() 或 PlayOneShot()。但不幸的是,它不是很准确。所以我想,我可以用 PlayScheduled() 和缓冲时间来实现它。就像在这个例子中一样:http ://www.schmid.dk/talks/2014-05-21-Nordic_Game_Jam/Schmid-2014-05-21-NGJ-140.pdf 但这也不起作用,我也没有声音完全没有,或者声音有时会被切断,就好像声音的开头没有播放一样。为什么没有播放预定的声音?
到目前为止,这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class inaccurateMetronome : MonoBehaviour {
public double bpm = 140.0F;
public AudioClip sound0;
public AudioSource audio0;
bool running = false;
bool ticked = false;
public double buff = 0.2d;
double timePerTick;
double nextTick;
double dspTime;
void Start () {
double startTick = AudioSettings.dspTime;
nextTick = startTick + buff;
audio0 = GetComponent<AudioSource>();
audio0.clip = sound0;
}
public void toggleMetronome() {
if (running)
stopMetronome();
else
startMetronome();
}
public void startMetronome() {
if (running) {
Debug.LogError("Metronome: already running");
return;
} else {
running = true;
timePerTick = 60.0f / bpm;
nextTick = AudioSettings.dspTime + buff;
Debug.Log("Metronome started");
}
}
public void stopMetronome() {
if (!running) {
Debug.LogError("Metronome: not yet running");
return;
} else {
running = false;
Debug.Log("Metronome stopped");
}
}
public void setBpm(double bpm){
this.bpm = bpm;
this.timePerTick = 60.0f / bpm;
}
void FixedUpdate() {
dspTime = AudioSettings.dspTime;
if ( running && dspTime + buff >= nextTick) {
ticked = false;
nextTick += timePerTick;
}
else if ( running && !ticked && nextTick >= AudioSettings.dspTime ) {
audio0.PlayOneShot(sound0, 1);
Debug.Log("Tick");
ticked = true;
}
}
}
如果有人可以帮助我,那就太好了。谢谢!