Android Soundpool Load(String path, int priority)

标签 android audio resources soundpool

我正在尝试从 android 加载声音。 声音在 res/raw/myownsound.wav 下。 我知道我已经可以使用以下方式加载声音:

 soundPool.load(context, R.raw.myownsound, 1)

出于自定义目的,我想使用以下方式加载它:

 soundPool.load("res/raw/myownsound", 1)

...但我收到以下错误:error loading res/raw/myownsound。 我还尝试了以下方法:

 soundPool.loadSound("android.resource://upg.GraphismeBase/raw/myownsound", 1)

.. 但我也收到错误消息:error loading android.resource://upg.GraphismeBase/raw/myownsound

soundPool.load(path, priority) 的正确使用方法是什么?

最佳答案

在你的项目中创建文件夹结构

/assets/sounds/data/

然后将您的 wav 文件复制到那里。

// Declare globally if needed
int mySoundId;
SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0 );
AssetManager am = this.getAssets();

//Use in whatever method is used to load the sounds
try {
    mySoundId = soundPool.load(am.openFd("sounds/data/myownsound"), 1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

可以试试这个(不确定它是否有效):

SoundPool mySoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    int myAudioFile = getResId("claps", R.raw.class);
    try{
        mySoundPool.load(context.getActivity(),myAudioFile,1);
    } catch (Exception e){
        message = String.valueOf(e);
    }

public static int getResId(String variableName, Class<?> c) {
    Field field = null;
    int resId = 0;
    try {
        field = c.getField(variableName);
        try {
            resId = field.getInt(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resId;
}

关于Android Soundpool Load(String path, int priority),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094370/

相关文章:

java - 如何防止键盘和菜单同时显示?

android - 为 SDK 24 构建安全吗?

java - 我正在尝试在我的游戏中使用 Unity Ads,但不起作用

android - 检查图像是否成功下载或失败

c# - 看到玩家时,敌人的音频和 Sprite 表不会启动/更改

android - 在 R.Raw 中打开文件音频

audio - 如何使用 FFMPEG 从没有音频的图像转换 MP4 视频文件?

c# - 国际化的最佳实践 : What is the best way to store exception/error or informational messages in C#.net?

c# - 如何解决此 "Resource name is not a valid identifier"编译器警告

并行部署期间 Tomcat 性能下降