从 android 中的 xml 数组获取资源

Get resources from xml array in android

我在 xml 文件中有一个数组:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="sounds_array">
        <item>R.raw.fart</item>
        <item>R.raw.beep33</item>
    </array>
</resources>

我想创建一个媒体播放器来播放我的 raw 文件夹中的这些声音。

mSoundsArray = mContext.getResources().obtainTypedArray(R.array.sounds_array);


mediaPlayer = MediaPlayer.create(mContext, mSoundsArray.getResourceId(0, -1));
                mediaPlayer.start();

但我得到的不是预期结果,而是这个错误:

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff

知道如何从这个数组中得到 resourceId 吗?

这就是我做同样的事情的方式:

 Resources res = getResources();
        final TypedArray sounds = res.obtainTypedArray(R.array.sounds_array);
        int[] resIds = new int[sounds.length()];
        for (int i = 0; i < sounds.length(); i++) {
            resIds[i] = sounds.getResourceId(i, -1);
        }
sounds.recycle();

变化:

 <array name="sounds_array">
    <item>R.raw.fart</item>
    <item>R.raw.beep33</item>
</array>

<string-array name="select_sounds">       
   <item>@raw/fart</item>
   <item>@raw/beep33</item>
</string-array>