1

I have an array in xml file:

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

I want to create a media player to play these sounds from my raw folder.

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


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

but istead of expected result I got an this error:

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

any idea how can I get resourceId from this array?

1 Answer 1

3

This is how i did the same sounds thing :

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

Change :

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

To

<string-array name="select_sounds">       
   <item>@raw/fart</item>
   <item>@raw/beep33</item>
</string-array>
Sign up to request clarification or add additional context in comments.

3 Comments

I get two elements -1 in resIds. I should add .mp3 to every <item>R.raw.beep33</item> ?
I am not able to access items in raw folder using <item>@raw/fart</item>. any idea why is that?
@SAIR it wont autocomplete, but just type the full name and it should be fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.