Play, pause, or stop a single sound effect

Page update date :
Page creation date :

Verification environment

Windows
  • Windows 11
Unity Editor
  • 2021.3.3f1
Input System Package
  • 1.3.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

About the material included with the sample

Sound effects are borrowed from the following sites.

About audio files

The following audio file formats can be played with Unity's standard features: Please prepare it in advance because it will be used in these tips.

  • WAV (.wav)
  • OggVorbis (.ogg)
  • MPEG layer 3 (.mp3)

For more information, see the official Unity documentation.

About playing sound effects

There are several ways to play sound effects, but here we want to walk you through a simple way to play them while using the standard features.

The content is to arrange a button and play, pause, and stop by clicking the button. In an actual game, you can implement the same process other than buttons.

First, place a button to play the sound effect as shown in the figure.

Add the prepared sound file by dropping it into your project.

Next, add "Audio Source" to the hierarchy.

When you select the Audio Source you created, the Audio Source component has an Audio Clip, into which you drop an audio file from your project to configure it.

By the way, if you start the game as it is, the sound effect you set will suddenly play. This is because "Play at game start" is checked. It is unlikely that the sound effect will be played suddenly, so leave this check unchecked.

Next, create a script. SoundPlayer Leave the name as .

The script looks like this:

using UnityEngine;

public class SoundPlayer : MonoBehaviour
{
  [SerializeField] private AudioSource AudioSource;

  public void OnClickPlay()
  {
    // オーディオを再生します
    AudioSource.Play();
  }

  public void OnClickPause()
  {
    // オーディオを一時停止します
    AudioSource.Pause();
  }

  public void OnClickStop()
  {
    // オーディオを停止します
    AudioSource.Stop();
  }
}

AudioSource will be set later from the editor. This AudioSource is the one with the sound effect set earlier.

AudioSource, , and methods, so you can play, pause, and PlayPauseStop stop respectively.

You can attach the script to the Audio Source, but in this case you'll attach it to the EventSystem. Make sure your script is populated with the Audio Source you created earlier.

Now assign each method to the click event of the three buttons.

Once you're done, run the game and see if the sound effects play.

Play, pause, and stop are commonly used terms, so you don't need to write them in detail, but I think they work as follows.

  • When you click the play button, the sound effect is played only once and played to the end.
  • During playback, click the pause button to stop the sound being played
  • When you click the play button while paused, the sound is played from the stopped position.
  • Click the stop button during playback to stop the sound being played
  • Click the play button while stopped to play from the beginning
  • Clicking the stop button during pause will release the paused position and the next time you play, it will be from the beginning.
  • If you click the play button during playback, the currently playing sound is stopped and played from the beginning.

Disadvantages of using AudioSource alone

There is a workaround, but this Tips has the following disadvantages. We'll cover these solutions in another tip.

Can't play two or more of the same sound at the same time

You can check this by pressing the play button multiple times, but the previous sound disappears when you play a new sound. Speaking of disadvantages, it is a disadvantage, but on the contrary, if you play the same sound on top of each other, the volume will be very loud, so it is an advantage in the sense that it prevents it. However, the previous sound will still disappear, so depending on the type of sound, it will be unnatural.

You must have as many AudioSources as there are types of sounds to play

Since only one audio clip can be set in an AudioSource, you need to provide an AudioSource as you have as many sounds as you want. You can also prepare only one AudioSource and change the audio clip when playing a different sound. It is unnatural because the previous sound disappears due to the problem that two or more of the same sound written before cannot be played at the same time.