Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx get duration of a Music

Tags:

java

time

libgdx

Im trying to get the duration of a Music, in libgdx

Im not talking about :

getPosition();

What I want is the Duration of the music. (in seconds)

Thanks

like image 252
LeSam Avatar asked Jan 06 '14 02:01

LeSam


2 Answers

To be clear by "duration" you mean the total time the music would play without looping.

I don't think the Libgdx sound APIs expose this (the lowest common denominator sound API across desktop, Android and Web is pretty low ...).

You may be able to get this information out of the files you're loading into system, though (e.g., if they're wav or mp3 files there should be APIs to query their duration).

like image 93
P.T. Avatar answered Oct 13 '22 01:10

P.T.


Its an old question but I will give my answer for the future visitors

To get the duration of a music file you can use the gdx-audio extension.

    FileHandle file=Gdx.files.internal(filepath);
    FileHandle external=Gdx.files.external("mygame/"+filepath);
    if(!external.exists())file.copyTo(external); //copy the file to the external storage 
    Mpg123Decoder   decoder = new Mpg123Decoder(external);
    System.out.println("name:"+filepath+"  duration:"+decoder.getLength());
like image 34
SteveL Avatar answered Oct 12 '22 23:10

SteveL