Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AudioSource.Play

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function Play(delay: ulong = 0): void;
public void Play(ulong delay = 0);

Parameters

delay Delay in number of samples, assuming a 44100Hz sample rate (meaning that Play(44100) will delay the playing by exactly 1 sec).

Description

Plays the clip with an optional certain delay.

The delay parameter is deprecated, please use the newer PlayDelayed function instead which specifies the delay in seconds.

Note: To obtain sample accuracy with an AudioClip with a different samplerate (than 44.1 khz) you have to do the math yourselves. Delaying an audiosource with an attached AudioClip with samplerate of, say, 32 khz, with 16k samples(.5 sec) is done by Play(22050). ((44100/32000) * 16000 = 22050).

Note: The AudioSource.PlayScheduled API will give you more accurate control over when the audio clip is played.

#pragma strict
@RequireComponent(AudioSource)
function Start() {
	var audio: AudioSource = GetComponent.<AudioSource>();
	audio.Play();
	audio.Play(44100);
}
	using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))] public class ExampleClass : MonoBehaviour { void Start() { AudioSource audio = GetComponent<AudioSource>(); audio.Play(); audio.Play(44100); } }

See Also: Stop, Pause, clip and PlayScheduled functions.