- Home /
Play audio clip after timer
I want to play an audio clip after a certain amount of time, for example, after 1,800 seconds(30 min.) I want to play an audio clip of the player complaining that he is bored. Can someone help me with this please?
Answer by Sinhabros · Jul 10, 2015 at 08:36 AM
Here is a simple (very simple) timer script that you could use by setting it to 30 seconds. When it is over, you can call AudioSource.Play() method to play the music clip.
using UnityEngine;
public class Timer
{
public float time = 30f; //30 seconds for you
public void Update()
{
if (time > 0) {
time -= Time.deltaTime;
}
else {
Debug.Log("Play Audio Here -- Timer Over!!");
GetComponent<AudioSource>().Play();
}
}
Answer by spooneystone · Jul 09, 2015 at 10:29 PM
Use time.time it will give you the time since the game started . Then use something like this
Float timer;
timer = time.time;
If( timer > 30.00f)
Play();
It is actually saying I need a semicolon on line 5,14 but when I put one there it just gives me more errors
Answer by vividhelix · Jul 09, 2015 at 10:53 PM
Here is what I use in my projects:
public static class MonoBehaviourExtensions
{
public static void InvokeLater(this MonoBehaviour monoBehaviour, float delay, Action action)
{
monoBehaviour.StartCoroutine(_invoke(delay, action));
}
private static IEnumerator _invoke(float delay, Action action)
{
if (delay > 0f)
{
yield return new WaitForSeconds(delay);
}
action();
yield return null;
}
}
You can then use this in any monobehavior like this:
this.InvokeLater(30, MethodNameHere)
or
this.InvokeLater(30, ()=>MethodNameHere())
or
this.InvokeLater(30, ()=>{MethodNameHere();}
where MethodNameHere
does whatever you want it to do in 30 seconds.
EDIT: I just wanted to point out this is just a refactoring-friendly version of MonoBehavior.invoke
: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html . You can use that but it requires the method name as a string which always irked me.
Answer by ransomink · Jul 10, 2015 at 03:47 AM
Dunno what language you need or prefer, but I use JavaScript (UnityScript)...so...yeah. The first method uses StartCoroutine: this is a good/great? workaround if you need to use a parameter(s).
public var m_complainTime : float;
public var m_complainClip : AudioClip;
private var _audio : AudioSource;
function Start ()
{
_audio = GetComponent(AudioSource);
StartCoroutine(PlayDelayedClip(m_complainTime, m_complainClip));
}
public function PlayDelayedClip(time : float, clip : AudioClip)
{
yield WaitForSeconds(time);
_audio.PlayOneShot(clip);
}
However, when using JavaScript it's not necessary to use StartCoroutine, so you can call the function as so:
function Start ()
{
_audio = GetComponent(AudioSource);
PlayDelayedClip(m_complainTime, m_complainClip);
}
Or you could do it this way, though using Update() is not preferred since it runs every frame:
public var m_complainTime : float;
public var m_complainClip : AudioClip;
private var _hasPlayedClip : boolean;
private var _timer : float;
private var _audio : AudioSource;
function Start ()
{
_audio = GetComponent(AudioSource);
_hasPlayedClip = false;
_timer = 0;
}
function Update ()
{
if (!_hasPlayedClip)
{
PlayDelayedClip(m_complainTime, m_complainClip);
}
}
public function PlayDelayedClip(time : float, clip : AudioClip)
{
if (_timer < time)
{
_timer += Time.deltaTime;
Debug.Log(_timer);
}
else
{
_timer = 0;
_hasPlayedClip = true;
_audio.PlayOneShot(m_complainClip);
}
}
Hope this helps. This can easily be translated into C# as well...
Answer by NeverHopeless · Jul 10, 2015 at 04:51 AM
Try like this:
// Schedule function call after 30 secs
Invoke("PlayAudioClip", 30.0f);
// Method that plays sound
void PlayAudioClip()
{
myAudioSource.Play();
}
// If you want to cancel this function call you can try like:
CancelInvoke("PlayAudioClip");
Hope it helps!
Your answer
Follow this Question
Related Questions
timer starts in the menu scene 1 Answer
Convert this string formatting from C# to JS 1 Answer
Start timer with trigger 1 Answer
I want particle to emit for one second after my mouseclick 1 Answer