Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


time

Playback position in seconds.
Read time 1 minuteLast updated 4 days ago

Definition

  • Type: Property
  • Namespace: UnityEngine
  • Assembly: UnityEngine.AudioModule
public float time { get; set; }

Remarks

Use this property to read the current playback time or to seek to a new playback time.
If the audio source is not playing, AudioSource.time will always return 0, regardless of any previously set or expected playback position.
If you need to know the playback position when the audio source is not playing, use AudioSource.timeSamples instead.
Setting this property while the audio source is not playing will update the playback position, but it will only take effect once playback begins.
Be aware that: On a compressed audio track, the reported playback position does not necessarily reflect the exact time in the track.
Compressed audio is stored in packets, and the size of these packets depends on the compression settings. In many cases, a single packet can represent 2–3 seconds of audio.

Examples

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ AudioSource audioSource; void Start() { audioSource = GetComponent<AudioSource>(); } void Update() { if (Input.GetKeyDown(KeyCode.Return)) { audioSource.Stop(); audioSource.Play(); } Debug.Log(audioSource.time); }}