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

8

41

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?

Bold answered 21/9, 2011 at 12:17 Comment(2)
i need a small info, how many audio files you have kept in res folder and what's the total size of those mp3 files. thanks:)Adduce
You need to add more information to this question. What file types you are trying to play and what the error/stack trace message is you are receiving.Augite
S
59

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();
            }
        });
Shively answered 21/9, 2011 at 12:27 Comment(7)
I got the ResourceNotFound exception while using like this. I have used getResources().getIdentifier(audioname, "res/raw", getPackageName());.Bold
don't use res/raw. use like this getResources().getIdentifier(audioname, "raw", getPackageName());Shively
Still same problem and exceptionBold
can you show how you are displaying text in listview. eg. if raw folder contains file like sound.mp3 then you should display the file name as 'sound' only, don't display file extension.Shively
now i edited the answer in this line String fname=txtView.getText().toString().toLowerCase();Shively
Perfect. :) I passed that mp3 extension also. this is the problem.Bold
What is txt_view in R.id.txt_view? I am getting a type error whenever I try this.Rid
W
34

try this for playing from raw ::

MediaPlayer mPlayer2;
mPlayer2= MediaPlayer.create(this, R.raw.bg_music_wav);
        mPlayer2.start();

permission in manifest file ::

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Update::

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {     
  MediaPlayer mPlayer2;
if(position==1)
{
    mPlayer2= MediaPlayer.create(this, R.raw.song1);
            mPlayer2.start();
}else it() .....
}
Woods answered 21/9, 2011 at 12:20 Comment(10)
Yeah. I can play by using MediaPlayer.create(...). This is not a problem. My problem is i want to play particular audio file when select from ListView. Because Lot of files available on res\raw folder.Bold
as i mention in code "bg_music_wav.wav" you can play any fileWoods
one more thing you need to put code in click listener and apply your own logicWoods
For example, I have 1.mp3,2.mp3 up to 100.mp3. Now user select 49.mp3 in ListView. How to play that particular file?Bold
why you not try to load file in server and ply from it?Woods
This is off mode application.Bold
becase it is easy in your case to hadle 100 file because it is boringWoods
what do you mean off mode application.? elaborate please.Woods
Here I mention off mode means without internet connection.Bold
No need to add any permissionPloy
G
6
mVideoView = (VideoView) findViewById(R.id.Video_FrontPage);
uri = Uri.parse("android.resource://com.urPackageName/" + R.raw.welcom_video);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
Gladi answered 21/9, 2011 at 12:54 Comment(1)
I am unable to access R.raw.filename. I have made a raw folder in my res folder and have also put my file in it.Jacquesjacquet
C
4
 lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String product = ((TextView) view).getText().toString();

          int [] resID= {R.raw.sound1,R.raw.sound2,R.raw.sound3};
        MediaPlayer mediaPlayer=MediaPlayer.create(this,resID[position]);

          mediaPlayer.start();
          // sending data to new activity


      }
    });
}
Condom answered 13/7, 2013 at 18:12 Comment(0)
C
2
var mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1)
mediaPlayer.start() // no need to call prepare(); create() does that for you
Carrycarryall answered 21/11, 2021 at 12:15 Comment(0)
U
1

Place your audio file in the res/raw folder and call it from the create method of MediaPlayer.

MediaPlayer.create(this, R.raw.audio_file_name).start();

Eg:

MediaPlayer.create(this, R.raw.sample).start();
Useful answered 28/2, 2021 at 13:10 Comment(0)
V
0

To play audio from the raw folder call this method. In my case my file name was notificaion.mp3:

AudioPlayer().playAudio(mContext, "notificaion")

Here is the AudioPlayer class:

class AudioPlayer {
private var mMediaPlayer: MediaPlayer = MediaPlayer()

private fun stopAudio() {
   try {
       mMediaPlayer.release()
   }catch (ex: Exception){
       ex.printStackTrace()
   }

}

fun playAudio(mContext: Context, fileName: String) {
    try {
        stopAudio()
        mMediaPlayer = MediaPlayer.create(mContext, mContext.resources.getIdentifier(fileName, "raw", mContext.packageName))
        mMediaPlayer.setOnCompletionListener {  stopAudio() }
        mMediaPlayer.start()
    }catch (ex: Exception){
        ex.printStackTrace()
    }

    }
}
Vidar answered 7/10, 2019 at 6:57 Comment(0)
B
0
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();
            }
        });

add this code in onItemClickListener and one more information is you can't play any file other than mp3 and the mp3 file must be the original extension as file.

Billingsgate answered 25/1, 2022 at 15:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.