Search code examples
libgdxdurationoggaudio

How to get an ogg's total duration?


I want to make my music fade in and out between songs. So I must get the music's total duration.

How do I get an Ogg's duration?

I've researched and it seems you need Mpg123Decoder but it doesn't exist. What am I doing wrong?

UPDATE: I tried this (it might be that I am doing something wrong with the external paths):

FileHandle list = Gdx.files.internal("./bin/sounds/music");
for (FileHandle files : list.list("ogg")) {
        FileHandle f = Gdx.files.external(Game.EXTERNALPATH + "sounds/music/" + files.name());
        if (!f.exists()) {
            files.copyTo(f);
        }
        musicList.add(f);
        musicListInternal.add(files);
    }

So the paths are:

Nulled/sounds/music/Rainbows and Unicorns.ogg
Nulled/sounds/music/The Stage is Set.ogg
Nulled/sounds/music/Victory Theme (Faded).ogg

And

            currentMusic = Gdx.audio.newMusic(musicListInternal.get(currentPlayingIndex));
        mpgDecoder = new Mpg123Decoder(musicList.get(currentPlayingIndex));
        totalDuration = mpgDecoder.getLength();
        mpgDecoder.dispose();
        currentMusic.play();

But It gives me an error:

    LwjglGraphics: created OpenGL 3.2+ core profile context. This is experimental!
3 musics loaded!
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000126f1d30, pid=8116, tid=3668
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b15) (build 1.8.0_45-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [gdx-audio64.dll+0x1d30]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\PhysiOS\Dropbox\Programming\workspace\nulled\desktop\hs_err_pid8116.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
AL lib: (EE) alc_cleanup: 1 device not closed
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:717] error: Encountered free format header, but failed to guess frame size.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:717] error: Encountered free format header, but failed to guess frame size.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:717] error: Encountered free format header, but failed to guess frame size.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 65688.
Note: Trying to resync...
Note: Skipped 757 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 66446.
Note: Trying to resync...
Note: Skipped 55 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 66502.
Note: Trying to resync...
Note: Skipped 32 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 66535.
Note: Trying to resync...
Note: Skipped 90 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 66626.
Note: Trying to resync...
Note: Skipped 939 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 67566.
Note: Trying to resync...
Note: Skipped 14 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1040] error: Giving up searching valid MPEG header after (over) 64K of junk.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:650] error: tried to decode obviously invalid header
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 67581.
Note: Trying to resync...
Note: Skipped 1024 bytes in input.
[/var/lib/jenkins/workspace/libgdx/extensions/gdx-audio/jni/libmpg123/parse.c:1121] error: Giving up resync after 1024 bytes - your stream is not nice... (maybe increasing resync limit could help).
Trouble with mpg123: Failed to find valid MPEG data within limit on resync. (code 28)

UPDATE: Wrong decoder, used VorbisDecoder instead.


Solution

  • The Mpg123Decoder class comes with the audio extension ,unfortunately this extension is deprecated.Since it seems that the jars have been removed from the libgdx project extensions page and I couldn't find anything anywhere else on google, I uploaded a zip here, Its an old version but I believe it will work just fine with the new versions of libgdx.

    To use it you need first to copy the music file from the application's folder to a location on the external memory

    public static Mpg123Decoder getDecoder(String musicFile){
        FileHandle file=Gdx.files.internal(musicFile);
        FileHandle external=Gdx.files.external("myappexternalfolder/"+file.name());
        if(!external.exists())file.copyTo(external); //copy the file to the external storage only if it doesnt exists yet
        return new Mpg123Decoder(external);
    }
    
    public float getMusicFileLength(String musicFile){
          return getDecoder(musicFile).getLength();
    }
    

    It returns a float, for example 10.5=>ten and a half seconds