如何获得ogg的总持续时间?

时间:2015-07-29 13:20:27

标签: libgdx duration ogg audio

我想让我的音乐在歌曲之间淡入淡出。所以我必须得到音乐的总持续时间。

如何获得Ogg的持续时间?

我已经研究过,似乎你需要Mpg123Decoder,但它并不存在。我做错了什么?

更新: 我试过这个(可能是我在外部路径上做错了):

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);
    }

所以路径是:

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

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

但它给了我一个错误:

    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)

更新:错误的解码器,改为使用VorbisDecoder。

1 个答案:

答案 0 :(得分:0)

Mpg123Decoder类带有音频扩展名,不幸的是这个扩展名已被弃用。因为似乎已经从libgdx项目扩展页面中删除了jar并且我在google上找不到任何其他内容,我上传了一个zip { {3}},它是一个旧版本,但我相信它可以与新版本的libgdx一起使用。

要使用它,首先需要将音乐文件从应用程序的文件夹复制到外部存储器上的某个位置

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();
}

它返回一个浮点数,例如10.5 =>十秒半