If AudioSource is playing a specific Sound ?

Is it possible to check if AudioSource is playing a certain clip and then execture a code if it is?

Interesting question - my initial thought was that you could do this using clip and isPlaying, such as:

if(AudioSource.clip == clipToTest && AudioSource.isPlaying) { ... }

However, clip gives you only the default clip assigned to an AudioSource, which is not necessarily the clip it is playing at the moment (if you were using PlayOneShot, for example). So I’m tempted to say that there’s nothing built-in to do this, and you’ll have to maintain your own variable to keep track of what clip you’ve most recently called to play on the AudioSource.

Hey I know that this like four years later but if anyone is still searching this, I use a simple method to do just this.

private AudioClip clip, currentClip;

private void PlayClip(AudioClip clip)
    {
        if (currentClip != clip) //checks if the provided clip is still playing
        {
            src.Stop(); //if not, it stops playback and changes the clip
            currentClip = clip;
            src.PlayOneShot(currentClip);
        }
        else
        {//otherwise, it checks if the src is currently playing the audioclip and plays it if it isn't
            if (!src.isPlaying)
            {
                src.PlayOneShot(currentClip);
            }
        }
    }

This version looks for a sound in a list of sound clips and returns true if the sound is playing. I use it for scene transitions, so if the two scenes have the same background music the music doesn’t restart from the beginning.

public bool IsThisSoundPlaying(int newSound)
//Check what sound is playing so we only stop it if the sound changes
{
        Sound s = themeTunes[newSound];
        if (s.source.isPlaying)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

I wrote a little OneShotAudioManager that I use in our current project. It allows for preventing the same audioClip from playing while it is still playing(If the click flagged to do so). It needs to be refactored for random pitch variants, and could probably make use of some AudioSource pooling(which would also need a way to position the audiosource as well as assign the appropriate AudioGroup).

    //  TO USE: Add to the script that needs to play the audio: 
    //  [SerializeField] private AudioData m_nameOfAudioData;
    //  Set data in inspector
    //  To fire off the audio: 
    //  OneShotAudioManager.PlayOneShotAudio(m_nameOfAudioData);
    
        using System.Collections.Generic;
        
        using UnityEngine;
        
        namespace Project.Audio
        {
            public static class OneShotAudioManager
        	{	
        		private static List<AudioData> s_oneShotAudioSourceClipData = new List<AudioData>();
        
        		public static void PlayOneShotAudio(AudioData audioData)
        		{
        			if (!s_oneShotAudioSourceClipData.Contains(audioData))
        			{
        				s_oneShotAudioSourceClipData.Add(audioData);
        			}
        
        			// prevent an audio clip from playing if it isn't supposed to:
        			if (!audioData.audioClipData.allowMultipleOneShot && !IsOneShotAudioReady(audioData))
        			{
        				return;
        			}
        
        			// Log the time for tracking:
        			audioData.audioClipData.lastStartTime = Time.time;
        
        			audioData.audioSource.PlayOneShot(audioData.audioClipData.audioClip, audioData.audioClipData.volume);
        		}
        		
        		public static bool IsOneShotAudioReady(AudioData audioData)
        		{
        			// No key for audioSource so it must be not playing:
        			if (!s_oneShotAudioSourceClipData.Contains(audioData)) 
        			{
        				s_oneShotAudioSourceClipData.Add(audioData);
        			}
        
        			return ((Time.time - audioData.audioClipData.lastStartTime) >= audioData.audioClipData.audioClip.length) || (audioData.audioClipData.allowMultipleOneShot);
        		}
        	}
        
            [System.Serializable]
            public class AudioData
            {
                #region Inspector Assigned Field(s):
                [SerializeField] private AudioSource m_audioSource;
                [SerializeField] private AudioClipData m_audioClipData;
                #endregion
        
                #region Properties:
                public AudioSource audioSource => m_audioSource;
                public AudioClipData audioClipData => m_audioClipData;
                #endregion
            }
        
            [System.Serializable]
            public class AudioClipData
            {
                #region Inspector Assigned Field(s):
                [SerializeField] private AudioClip m_audioClip;
                [SerializeField, Range(0.1f, 1f)] private float m_volume;
                [SerializeField] private bool m_allowMultipleOneShot = false;
                [HideInInspector] public float lastStartTime;
                #endregion
        
                #region Properties:
                public AudioClip audioClip => m_audioClip;
                public bool allowMultipleOneShot => m_allowMultipleOneShot;
                public float volume => m_volume; 
                #endregion
            }
        }