0

I have a list of music (.mp3) that I want to be played with ImageButton and I want to play each sound with its ID

First, I have a database that has field _id. Then, I tried to

 final String ambil = kata.get(3)

for retrieving the _id from query that I used. Then, I used

speaker.setTag(ambil);

speaker is an ImageButton that I want to tag each button with its _id so it will make sound different each other.

Then, I ordered all of the sound file that I have with this

int names[] = {R.raw.a,R.raw.a,R.raw.c};

Finally, I want to create a MediaPlayer with this

MediaPlayer mp = MediaPlayer.create(LazyAdapter.this, R.raw.names[ambil]);

Is my algorithm right ?

  • how does Your array look like? Is it a String Array with only the mp3 names? instead You can do a integer array like int[]names={R.raw.baby,R.raw.forest}; And then pass it like MediaPlayer.create(this,names[0]) or MediaPlayer.create(this,names[1]) – Opiatefuchs Jun 26 '14 at 07:33
  • I get what you mean, but I get an error that shows the method create(Context,URI) in the MediaPlayer is not applicable for arguments. What does it mean ? – Michael Roring Jun 26 '14 at 08:34
  • something has passed wrong. The mediaPlayer could be created in some different ways. On is, to pass the URI of a file, another is to pass the resource. Can You please show me how You have done this? – Opiatefuchs Jun 26 '14 at 08:44
  • I have edited my question. I hope you can understand these codes. For your information, this is not in Activity, just only a class – Michael Roring Jun 26 '14 at 08:58
  • ah, oh no that´s not what I mean...I will write an Answer to explain it better – Opiatefuchs Jun 26 '14 at 09:00

2 Answers2

0

You can give this a try:

Firstly, get a resource id using file name:

int rid = context.getResources()
        .getIdentifier("filename","raw", context.getPackageName());

Then get the file in input stream:

InputStream iS;
iS = getResources().openRawResource(rID);

You can refer this post for more explanation.

Another post for reference

Hope it helps.

Community
  • 1
  • 1
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

For now, I will give a hint how it could be done in a simple way. You can create an array with raw resources:

 int names[] = new int {R.raw.a,R.raw.b,R.raw.c};

don´t make a String array like You have done it, this will not work. Use this example above and create the mediaplayer like this:

 MediaPlayer mp = MediaPlayer.create(LazyAdapter.this, names[0]);

EDIT

from Your example above, I see some wrong things.

First, You can´t pass a String to identify the number of an array. You did it like this:

     final String ambil = kata.get(3)

and You trying to get the resource for mediaPlayer like:

     MediaPlayer mp = MediaPlayer.create(LazyAdapter.this, R.raw.names[ambil]);

There You pass a string to get a resource from names[] ---> R.raw.names[ambil]]; Don´t write R.raw.names[], write only names[].

Also, You have to pass an integer to get a value from an array like:

      names[0]

The question is, which integer You get from Your database, You say You get an id, but what is the value? For explanation, if You write names[0], that means that You get the first value of the array (because arrays starts with 0). In Your case, you refer to the resource R.raw.a . If You want to refer to the second song, You have to write names[1].

I don´t know how You have Your database implemented and in what way You give the _id. But be sure You give the _id in the way, that it will not be higher than the number of resources. If You create an autoincrement id, than it will not work in the case, when the value is higher than the number of songs.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49