Play, pause, stop, and loop background music

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

BGM is borrowed from the following site.

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.

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

For more information, see the official Unity documentation.

About BGM playback

There are several ways to play background music, but here we want to explain how to play it in a simple way 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 BGM as shown in the figure.

Add the prepared audio 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 BGM you set will suddenly play. This is because "Play at game start" is checked. If you want to play it immediately at the start of the scene, there is no problem as it is, but this time it will not play immediately, so leave this check unchecked.

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

The script looks like this:

using UnityEngine;

public class MusicPlayer : 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 audio file you just inserted.

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.

After setting up, run the game and see if the background music plays.

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 background music 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.

Loop playback

It plays background music by default and stops automatically when it is played to the end. If you want to loop the background music and continue playing, check "Loop" in the Audio Source component and it will continue to play repeatedly until you press the stop button.

The content of the sound effect playback is the same

If you've already seen the tips on the sound effect side, you may have noticed that the sound effect and background music can be played with the same settings. Depending on the game framework, the processing of sound effects and background music may be separate, but Unity allows you to play both with the same processing.

About the phenomenon that background music pauses when the window is deactivated

By default, when other windows are active and the game screen is deactivated, the game itself is paused. Along with this, the playback of the background music is also suspended.

To avoid this, you can set it to run in the background in your project settings so that the background music is not interrupted.