Legacy Documentation: Version 2018.1 (Go to current version)
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

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> 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 method Play(delay: ulong = 0): void;
public void Play(ulong delay = 0);

Parameters

delayDeprecated. 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.

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

If AudioSource.clip is set to the same clip that is playing then the clip will sound like it is re-started. AudioSource will assume any Play call will have a new audio clip to play.

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

#pragma strict
// The Audio Source component has an AudioClip option.  The audio
// played in this example comes from AudioClip and is called audioData.
@RequireComponent(AudioSource)
public class ExampleScript extends MonoBehaviour {
	var audioData: AudioSource;
	function Start() {
		audioData = GetComponent.<AudioSource>();
		audioData.Play(0);
		Debug.Log("started");
	}
	function OnGUI() {
		if (GUI.Button(new Rect(10, 70, 150, 30), "Pause")) {
			audioData.Pause();
			Debug.Log("Pause: " + audioData.time);
		}
		if (GUI.Button(new Rect(10, 170, 150, 30), "Continue")) {
			audioData.UnPause();
		}
	}
}
using UnityEngine;

// The Audio Source component has an AudioClip option. The audio // played in this example comes from AudioClip and is called audioData.

[RequireComponent(typeof(AudioSource))] public class ExampleScript : MonoBehaviour { AudioSource audioData;

void Start() { audioData = GetComponent<AudioSource>(); audioData.Play(0); Debug.Log("started"); }

void OnGUI() { if (GUI.Button(new Rect(10, 70, 150, 30), "Pause")) { audioData.Pause(); Debug.Log("Pause: " + audioData.time); }

if (GUI.Button(new Rect(10, 170, 150, 30), "Continue")) { audioData.UnPause(); } } }

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

Did you find this page useful? Please give it a rating: