Docs / World
Music
MusicPlayer plays a game's score: one track at a time, looped, or a list of tracks in turn, faded in rather than started and faded out rather than cut. It is separate from AudioEngine, which plays sounds in the world, and it follows the player's music volume and background setting from PlayerSettings.
Not in the world
Music is not spatialized, on purpose. A score is not a sound in the world. A positional music source swings across the stereo field as the camera turns, which is the most common way background music goes wrong. MusicPlayer takes a file URL and a volume, and has no position.
It also does not go through AudioEngine's mix. It plays through its own AVAudioPlayer, so AudioEngine.masterVolume does not reach it; the player's main volume reaches it through apply(_:), below.
Playing a track
// your game let music = MusicPlayer() if let url = Bundle.main.url(forResource: class="p">"theme", withExtension: class="p">"m4a") { music.play(url) }
play(_:) plays one track, looped, fading it in over fadeIn (3 seconds). Asking for the track that is already playing does nothing, so you can call it every time the player enters an area without restarting the music. Asking for a different track fades the current one out first, then fades the new one in. The fades run one after the other, not as a crossfade.
stop(fade:) fades the score out, 0.6 seconds by default, and forgets it: the next play(_:) starts from the beginning.
A file that will not open leaves the game silent rather than broken.
A playlist
// your game
music.play(playlist: [explore, village, night])
play(playlist:) plays the tracks in order, and the list comes round again after the last. The first fades in; each one after it starts when the one before ends, at the same level, with no fade. A list of one track loops, exactly as play(_:) does, which is all play(_:) is. The same rules apply: the list already playing is left alone, a different list fades the current one out first, a track that will not open is passed over, and an empty list stops the score.
The end of a track is noticed by checking a few times a second, so expect a short gap between tracks. Playback is not gapless.
What it is doing
| Read | What it is |
|---|---|
playlist | the tracks asked for, whether or not they are playing |
track | the first of playlist |
playing | the first track of the list it was last set playing; nil once stopped or muted |
current() | async: the track the player is on right now, for a "now playing" line |
isAudible() | async: whether a track is playing right now, for a test |
playlist and track survive a mute, and playing does not. MusicPlayer is @Observable, so a SwiftUI settings view can watch volume, playlist, track and playing.
Volume and the player's settings
volume is 0…1, clamped. Zero stops the score instead of playing silence: a silent player would still be decoding, and a muted game should not do work the player asked it not to do. The track is remembered, and raising the volume again starts it, from the beginning.
init(volume:) takes a starting level, 0.45 by default. To follow the player's settings, call apply(_:) on both the sound engine and the music whenever they change:
// your game audio?.apply(settings) music.apply(settings) try settingsStore.save(settings)
MusicPlayer.apply(_:) sets volume to PlayerSettings.musicVolume times PlayerSettings.volume, so the music sits under the main volume, and sets playsInBackground from soundInBackground. musicVolume defaults to 0.45, below the effects: a score at the level of a sword drowns the fight it is scoring. The tests check that a main volume of 0.5 and a music volume of 0.6 give 0.3.
In the background
While the game is not the front app the score fades out over focusFade (0.3 seconds) and pauses. Coming back to the game fades it up again from the same place, rather than starting the track over. Set playsInBackground to keep it playing, for a player with the game on a second monitor.
focusFade is short because its only job is to keep the score from sounding cut off; much longer than a third of a second and it is still audible over the app the player switched to.
Holding it
hold() does the same on a game's say-so — under a pause menu, a cutscene: the score fades out over focusFade and pauses, keeping the list and the place in the track. release() carries on from there, fading back up. A play(playlist:) asked while held is kept for the release rather than started over it, and isHeld says which it is.
A world's own level
mix is the level the score is mixed at by whatever plays it, 0…1, 1 by default. What is heard is volume times mix (heard): the player's settings set volume, a world's music level sets mix, and neither overwrites the other, so a world mixed quiet stays quiet and the player's music slider still rules it. A mix of 0 stops the score just as a volume of 0 does.
Off the main thread
Starting the first audio player in a process brings up the audio session, and part of that is a slow synchronous check on whichever thread pressed play. On the main thread, that is a dropped frame on the frame the game starts its score. So every command goes to an actor, in the order it was sent: a stop and a play a moment apart can never arrive reversed and leave the game silent.
The properties change at once; the sound follows shortly after. settle() waits until every command sent so far has run. Await it in a test before checking what is audible, or before quitting so a fade finishes.
A world's music
A world file can name its own music, one track or several in turn. Soundscape plays it through a MusicPlayer you hand it:
let soundscape = Soundscape(audio: audio, music: MusicPlayer())
When Soundscape plays a world's sounds it sets the player's mix to the world's music volume, under the player's own volume, and starts the world's tracks as a playlist. pause() on the soundscape holds the world's music where it is, and resume() carries it on from the same place. stop() stops it and puts the mix back to 1. Music that follows play, such as a fight or danger, stays your game's: pass no music: and drive your own MusicPlayer. See worlds from a file.
What it does not do
- No crossfade between tracks. One fades out, then the next fades in.
- No layers or stems, no stingers, no beat-synced transitions and no ducking under dialogue.
- No gapless playback between playlist tracks.
- One track at a time per player.