정확한 메트로놈을 화합하려고합니다. 이미 몇 가지 질문이 있지만 내 문제에 대한 해결책을 찾을 수는 없습니다. 첫 번째 구현에는 Play() 또는 PlayOneShot()이 사용되었습니다. 그러나 불행히도 그것은 정확하지 않았습니다. 그래서 나는 PlayScheduled()와 버퍼 시간으로 구현할 수 있다고 생각했다. 이 예와 같이 : http://www.schmid.dk/talks/2014-05-21-Nordic_Game_Jam/Schmid-2014-05-21-NGJ-140.pdf 그러나 이것도 작동하지 않으며 전혀 소리가 들리지 않거나 소리가 시작되지 않는 것처럼 소리가 끊기는 경우가 있습니다. 예약 된 사운드가 재생되지 않는 이유는 무엇입니까? 누군가가 나를 도울 수 있다면Unity : PlayScheduled()가있는 정확한 메트로놈이 작동하지 않습니다.
가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;
}
}
}
그것은 환상적인 것 :
이 지금까지 내 코드입니다. 감사합니다.
답장을 보내 주셔서 감사합니다. 당신이 말한 것처럼 내 자신의 사운드를 연주하려했지만 "재생은 메인 스레드에서만 호출 할 수 있습니다."라는 오류 메시지가 나타납니다. 그렇다면 어떻게 OnAudioFilterRead에서 내 사운드를 구현할 수 있습니까? –