How to play the audio files directly from res/raw folder?

bharath picture bharath · Sep 21, 2011 · Viewed 96.7k times · Source

I have multiple audio files in res/raw folder. I showing ListView that contains audio files name. I want to play the corresponding audio file when user select into the ListView. I have used setDataSource(path), but it showing error while playing. How play the audio files directly from that folder? Or Is there any other way?

Answer

ilango j picture ilango j · Sep 21, 2011

add this code in onItemClickListener.

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {                 
                TextView txtView=(TextView)view.findViewById(R.id.txt_view);
                String fname=txtView.getText().toString().toLowerCase();

                                int resID=getResources().getIdentifier(fname, "raw", getPackageName());

                              MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
                    mediaPlayer.start();
            }
        });