How it works...

Audio Source components have a public readable property, isPlaying, which is a Boolean true/false flag, indicating whether the sound is currently playing. In this recipe, the text of the button is set to display Play Sound when the sound is not playing, and (sound playing) when it is. When the button is clicked, the ACTION_PlaySoundIfNotPlaying() method is called. This method uses an if statement, ensuring that a Play() message is only sent to the Audio Source component if its isPlaying is false, and updates the button's text as appropriate.