Skip to content

Playing, changing and stopping music

Ian edited this page Oct 13, 2024 · 4 revisions

The LibGDX standard for playing music, AssetManager.getAsset(name, Music.class).play() won't account for the user-specified volume and mute settings from UserSettings. To fix this, the ResourceService has been extended to add new music functionality that accounts for this, thus allowing calling classes to not worry about the user settings and prevent code repetition.

Playing music

Use the following method from the ResourceService:

public @Null Music playMusic(String musicName, boolean loop)

This method will play the music with the given path, assuming it wasn't playing already. The following will be handled for you:

  • Any previously playing music will be stopped
  • The Music asset will be fetched from the AssetManager and returned if you want to dispose of it later.
  • User-set volume and mute settings will be accounted for.

To stop music, use:

public void stopCurrentMusic()

Calls to the above functions will keep track of which music file is playing, so a call to stopCurrentMusic() will stop the currently-playing music.

Music in the GameArea

The MusicType enum in the MainGameArea stores music types used in-game:

public enum MusicType {
    NORMAL("bgm_new.wav"), BOSS("boss_room_music.mp3");
    ...
}

To add or change music files, simply change these path names or add new enum values. Then call MainGameArea.playMusic(MusicType musicType).

Be sure to reference your asset source in assets/sounds/music/music_citations.txt.

Table of Contents

Home

Design

Design Document

Design Choices

Game Wiki

Gameplay

Controls

Game Features

Utilities
Animals
Menus/screens
Character
Map
Weapon
Projectile
Items
Music/sound

User Guide

Starting the game

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Enhancement of Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally