0

I have this code for adding music from sdcard to my music player :

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/sdcard/");
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
}

but I want use music in raw folder in my music player not music in sdcard .... What can I do ?

Ehsan
  • 5
  • 8

2 Answers2

0

try this..

String path = "android.resource://" + getPackageName() + "/" + R.raw.yourfile;
williamj949
  • 11,166
  • 8
  • 37
  • 51
0

Please follow the below steps to achieve this :-

Step 1 :-

Create a raw folder inside res folder, and place the audio file inside that folder.

Step 2 :-

In your activity class, read from the raw folder in this way,

Field[] fields = R.raw.class.getFields();

// This is only if you have multiple files inside raw folder,else simply do

fields.getName()

to get the string file.

   for(int count=0; count < fields.length; count++){                
            int rid = fields[count].getInt(fields[count]);
            String filename = fields[count].getName();
            flist.add(filename);
        }

Step 3:-

Now that you have the file name, you can use it in your audio player.

String xyz = "android.resource://"+getPackageName()+"/raw/"+(file name);

Hope this helps.

williamj949
  • 11,166
  • 8
  • 37
  • 51
akash89
  • 881
  • 3
  • 12
  • 31