-1

I am trying to store resource identifiers for some mp3 files in an array. Here is the code.

    private HashMap<String,List<Integer>> listHashMapnum;
    private List<String> listhead;



    listhead.add("Forty");
    listhead.add("Fifty");
    ArrayList<Integer> fortysound = new ArrayList<>();
    fortysound.add(R.raw.forty);
    ArrayList<Integer> fiftysound = new ArrayList<>();
    fiftysound.add(R.raw.fifty);

Then I store the array list in a hashmap

 listHashMapnum.put(listhead.get(0),fortysound);
 listHashMapnum.put(listhead.get(1),fiftysound);

Then I use a function to get the value in the hashmap

 public Integer getChild2(int i, int i1) {
    return listHashMapnum.get(listheader.get(i)).get(i1);
}

I get the value from the function like this

 Integer childsound = (Integer) getChild2(i, i1);

I then use the childsound variable to initialize the mediaplayer

    Button bt;

    bt = (Button) view.findViewById(R.id.speaker);
    final MediaPlayer mp = MediaPlayer.create(context, childsound);
    bt.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            mp.start();
        }

    });

But the function returns a int value like "213736282" I found this out while trying to debug. But I don't understand why it does that and what I can do to get my resource identifier from the function. Any help is totally appreciated. Thank you.

Edit: The error was actually in another part of the code so the code above is correct. Thank you

slayer
  • 57
  • 2
  • 10
  • please see [**How to create a Minimal, Complete, and Verifiable example**](https://stackoverflow.com/help/mcve) – Ousmane D. Oct 14 '17 at 18:03
  • Post the declarations of all the variables used here, so we know their types. – Neo Oct 14 '17 at 18:05
  • You did get it. What makes you think there's a problem? – Oleg Oct 14 '17 at 18:06
  • @Oleg because the app keeps crashing at that point and when I try debugging I found out the childsound variable is having a value of int that looks this way "213585932" – slayer Oct 14 '17 at 18:19
  • What are you doing with `childsound`? the problem is probably there. – Oleg Oct 14 '17 at 18:21
  • I am using it to setup the mediaplayer here is the code – slayer Oct 14 '17 at 18:25
  • Button bt; bt = (Button) view.findViewById(R.id.speaker); final MediaPlayer mp = MediaPlayer.create(context, childsound); bt.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view){ mp.start(); } }); – slayer Oct 14 '17 at 18:25
  • And does it work when you pass `R.raw.forty` to it? What does it equal to? – Oleg Oct 14 '17 at 18:36
  • @Oleg thanks very much. I just did that and the app still crashed then I started checking other parts of the code and found the error was an iteration I made while debugging but didn't change it back which was an array that I added two items instead of one. Thanks very much buddy saved me a lot of time. – slayer Oct 14 '17 at 19:06

1 Answers1

0

Yes you are right. Android resource identifier are integer values which is generated and stored in R.java.

You can openRawResource method and read the resources

Check this answer for reading mp3 from raw resources

John
  • 8,846
  • 8
  • 50
  • 85