How to load music in Android with libGDX?

I want load mp3 song in an android app with libgdx library but i don't find the way to works. I don't know how programm this "issue".

Assets Manager could be the class for works with mp3 song?

I found also this way:


but in Android don´t work and desktop the same code work.

Update: Parse Method

public void parse() { JsonReader reader = new JsonReader(); JsonValue rootElem = reader.parse(file); JsonValue songDataElem = rootElem.get("songData"); JsonValue notesDataElem = songDataElem.get("notes"); JsonValue barsDataElem = songDataElem.get("bars"); JsonValue keysDataElem = songDataElem.get("keys"); JsonValue audioDataElem = rootElem.get("audioData"); NoteData[] notes = new NoteData[notesDataElem.size]; for (int i = 0; i < notesDataElem.size; i++) { notes[i] = new NoteData(notesDataElem.get(i).getInt("pitch"), notesDataElem.get(i).getFloat("time")); } BarData[] bars = new BarData[barsDataElem.size]; for (int i = 0; i < barsDataElem.size; i++) { BarData bar = new BarData(barsDataElem.get(i).getFloat("time")); bars[i] = bar; } char[] keys = new char[keysDataElem.size]; for (int i = 0; i < keysDataElem.size; i++) { keys[i] = keysDataElem.getChar(i); } float tempo = songDataElem.getFloat("tempo"); String file = audioDataElem.getString("file"); songData = new SongData(notes, bars, keys, tempo); parsed = true; }

and the constructor:

public SongFile(FileHandle file) { manager = new AssetManager(new ExternalFileHandleResolver()); file = Gdx.files.external(file.path());//30 if (file.exists()) { manager.load(file.path(), Music.class); manager.finishLoading(); music = manager.get(file.path(), Music.class); music.setLooping(true); music.play(); } }

In the main class:

String file = "/storage/emulated/0/download/prueba.mp3"; SongFile songFile = new SongFile(new FileHandle(file)); songFile.parse(); song = songFile.makeSong();


To load a Music instance we can do the following:

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

You can also use AssetManager to load your Music so that you can manage your assets in proper way.

AssetManager manager = new AssetManager(); manager.load("data/mymusic.mp3", Music.class); manager.finishLoading();

You can get your Music after assets have been successfully loaded.

Music music = manager.get("data/mymusic.mp3", Music.class);

Various Playback attributes that can use to play music


Check this thread if you've some particular problem on Android. Some times Sound may not play on Android devices but on desktop, it does play.


Add this permission to AndroidMainfest.xml file.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Make it clear for Android your targetSdkVersion is less than 23 if not take permission at run time by user before proceeding any work related to your File IO. For current targetSdkVersion check your defaultConfig of android build.gradle file, if not present their check AndroidManifest.xml file.

External Destination is address where we keep our own data like video, music and all.

Gdx.files.getExternalStoragePath() give path /storage/emulated/0/ in Android and User Directory on Desktop like this C:\Users\Abhishek Aryan\

/storage/emulated/0/ represent inbuilt Storage and Download is inside inbuilt storage.

if(Gdx.app.type==Application.ApplicationType.Android) { var assetManager = AssetManager(ExternalFileHandleResolver()) var fileHandle = Gdx.files.external("/Download/WorldmapTheme.mp3") if (fileHandle.exists()) { assetManager.load(fileHandle.path(), Music::class.java) assetManager.finishLoading(); var music = assetManager.get<Music>(fileHandle.path()) music.setLooping(true) music.play() } }

<strong>EDIT 2</strong>

This code working fine for me, Hopefully this will work for you

// code inside create() method of ApplicationListener

if(Gdx.app.getType()== Application.ApplicationType.Android) { String file = "/download/prueba.mp3"; FileHandle fileHandle = Gdx.files.external(file); SongFile songFile = new SongFile(fileHandle); songFile.parse(); song = songFile.makeSong(); }

Constructor of SongFile class

public class SongFile { AssetManager manager; Music music; public SongFile(FileHandle file){ manager = new AssetManager(new ExternalFileHandleResolver()); if (file.exists()) { manager.load(file.path(), Music.class); manager.finishLoading(); music = manager.get(file.path(), Music.class); music.setLooping(true); music.play(); } } ... }


  • Pinvoking adwapi.dll - cryptDecrypt and cryptEncrypt functions, weird problem
  • how to sync a midi file with an audio file
  • How to Eatchar in a multi-line Vim abbreviation
  • Why is the compiler showing error “expected expression before stud”?
  • GCM registration works for debug APK but not release APK
  • Android Login- checking if username and password match with MySQL database
  • I don't get what's the difference between format() and … (python)
  • GRPC and types import error in App Engine Datastore
  • How to define an array of floats in Shader properties?
  • How to extract text from a PDF and decode characters?
  • Scala: Function returning an unknown type
  • Access object instance inside an event handler
  • How to read piped content in C?
  • Possible to “watch” both HAML and SASS at the same time?
  • How to print columns containing value
  • NUnit 3.0 TestCase const custom object arguments
  • xcode don't localize specific strings
  • Create Instant using a negative year
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Is there a way to do normal logging with EureakLog?
  • Why querying a date BC is changed to AD in Java?
  • Alert pop up with LWUIT
  • Get one-time binding to work for ng-if
  • Fetching methods from BroadcastReceiver to update UI
  • Align navbar back button on right side
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • AES padding and writing the ciphertext to a disk file
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Window Size for Mac application
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Convert array of 8 bytes to signed long in C++
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Linker errors when using intrinsic function via function pointer
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Sorting a 2D array using the second column C++
  • How to push additional view controllers onto NavigationController but keep the TabBar?