gdx 播放 sound 失败

在Android 7.0设备上,使用GDX库进行游戏开发时遇到声音播放故障,首次加载sound资源无法正常播放。通过深入AndroidSound的play方法追踪,找到了问题的原因并提出了相应的解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

①
... 
<T extends Disposable> T getAssets(String fileName, Class<T> clazz) {
    if (!manager.isLoaded(fileName, clazz)) {
        Gdx.app.log(tag, clazz.getSimpleName() + " 资源未加载 " + fileName);
        manager.load(fileName, clazz);
        manager.finishLoadingAsset(fileName);
    }
    return manager.get(fileName, clazz);
}
...

②
...
public Sound getSound(String fileName) {
    return getAssets(fileName, Sound.class);
}
...

③
...
getSound("xxxxxxxxxxx").play();
...

环境

gdx 版本 1.9.2
  • pc 平台可以正常播放
  • android 平台(android 7.0)在程序首次加载该 sound 时不会播放声音。

原因

查看 android 日志,会打印如下日志

05-23 18:34:16.104  6469  6489 W SoundPool:   sample 4 not READY


android 部分代码 Sound com.badlogic.gdx.backends.android.AndroidAudio.newSound(FileHandle file)
    @Override
    public Sound newSound (FileHandle file) {
        if (soundPool == null) {
            throw new GdxRuntimeException("Android audio is not enabled by the application config.");
        }
        AndroidFileHandle aHandle = (AndroidFileHandle)file;
        if (aHandle.type() == FileType.Internal) {
            try {
                AssetFileDescriptor descriptor = aHandle.getAssetFileDescriptor();
                AndroidSound sound = new AndroidSound(soundPool, manager, soundPool.load(descriptor, 1));
                descriptor.close();
                return sound;
            } catch (IOException ex) {
                throw new GdxRuntimeException("Error loading audio file: " + file
                    + "\nNote: Internal audio files must be placed in the assets directory.", ex);
            }
        } else {
            try {
                return new AndroidSound(soundPool, manager, soundPool.load(aHandle.file().getPath(), 1));
            } catch (Exception ex) {
                throw new GdxRuntimeException("Error loading audio file: " + file, ex);
            }
        }
    }

使用 AssetManager 管理器加载 sound 本质上也是调用上面代码

追踪 AndroidSound 的 paly 代码后得出结论:

实例化 AndroidSound 完成的时候,其实 android 的音频解码器还未解码加载完成该 sound 时就调用 play 方法了。

解决

public void  seriousPlay(Sound sound){
    while (sound.play() ==-1 ) {
        // do nothing
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值