Hello all –
I’m trying to play an AudioSource backwards. I’ve done a bit of reading and can’t seem to figure out if this is an actual possibility.
What I’m currently trying to do is:
- First I start playing the audioSource.
- Then I set the audioSource.time equal to the audioSource.clip.length.
- Then I set the audioSource.pitch to -1.
However, this doesn’t seem to work.
Any ideas?
first of all, note that AudioSource.time may not actually represent the actual time of the clip (with compressed audio)
http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-time.html
I’m not really sure what the “correct” way would be to handle that line, probably using timeSamples vs clip.samples
to be safe, I would use one less
audio.timeSamples = audio.clip.samples - 1;
audio.pitch = -1;
audio.Play();
I’ve tested this and it works
but not without the “-1” in “audio.clip.samples - 1;” though, so I guess once you reach the last sample, it automatically “resets” or whatever, so keep that in mind…
Temporary Fix I came up with
using System.Collections;
using UnityEngine;
public class AudioPlayer : MonoBehaviour
{
private AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void PlayAudio(AudioClip audioClip)
{
audioSource.pitch = 1;
audioSource.clip = audioClip;
audioSource.Play();
}
public void PlayReverseAudio(AudioClip audioClip)
{
audioSource.pitch = -1;
audioSource.loop = true;
audioSource.clip = audioClip;
audioSource.Play();
StartCoroutine(StopLoop());
}
public IEnumerator StopLoop()
{
yield return new WaitForSeconds(1f);
audioSource.loop = false;
}
}