Skip to content

Streaming music

badlogic edited this page Sep 14, 2013 · 1 revision

For any sound that's longer than a few seconds it is preferable to stream it from disk instead of fully loading it into RAM. Libgdx provides a Music interface that lets you do that.

To load a Music instance we can do the following:

Music music = Gdx.audio.newMusic(Gdx.files.internal("data/mymusic.mp3"));

This loads an MP3 file called "mymusic.mp3" from the internal directory data.

Playing back the music instance works as follows:

music.play();

Of course you can set various playback attributes of the Music instance:

music.setVolume(0.5f);                 // sets the volume to half the maximum volume
music.setLooping(true);                // will repeat playback until music.stop() is called
music.stop();                          // stops the playback
music.pause();                         // pauses the playback
music.play();                          // resumes the playback
boolean isPlaying = music.isPlaying(); // obvious :)
boolean isLooping = music.isLooping(); // obvious as well :)
float position = music.getPosition();  // returns the playback position in seconds

Music instances are heavy, you should usually not have more than one or two at most loaded.

A Music instance needs to be disposed if it is no longer needed, to free up resources.

music.dispose();

Table of Contents

Wiki Style Guide

Developer's Guide

The Application Framework
A Simple Game

File Handling

Networking

Preferences

Input Handling

Memory Management

Audio
Graphics

    2D Graphics

    3D Graphics

Managing Your Assets

Internationalization and Localization

Utilities
Math Utilities
Tools
Extensions
Using libGDX With Other JVM Languages
Third Party Services

Articles

Clone this wiki locally
You can’t perform that action at this time.