Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play mp3 files from internal and external SD card in android?

I am working on a mp3 player app, which plays .mp3 files present anywhere inside an internal SD card.

I have used the following codes to fetch the .mp3 files present in internal storage.

ArrayList<File> inFiles = new ArrayList<File>();
File list[] = file.listFiles();
//Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++) 
{
    // myList.add( list[i].getName() );
    File temp_file = new File(file.getAbsolutePath(),list[i].getName());
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
    if (temp_file.listFiles() != null) 
    {
        //Log.i("inside", "call fn");
        listfiles(temp_file);

    }
    else 
    {
        if (list[i].getName().toLowerCase().contains(".mp3"))
        {
            inFiles.add(list[i]);
        //Log.e("Music", list[i].getName());
        }
    }
}

How do I similarly get the .mp3 files from external SD card as well?

like image 419
Adarsh H S Avatar asked Aug 17 '12 12:08

Adarsh H S


People also ask

How do you list MP3 audio files from storage and SD card on Flutter app?

To list and view Audio Files files from internal/external storage, you have to use flutter_file_manager, path, and path_provider_ex flutter package. Add the following lines in your pubspec. yaml file to add this package in your dependency. Add read / write permissions in your android/app/src/main/AndroidManifest.


1 Answers

You can get the root directory of the external sdcard using this code of line

File root = Environment.getExternalStorageDirectory();

Now you can do the same thing as you do for internal

ArrayList<File> inFiles = new ArrayList<File>();
File list[] = root.listFiles(); // here use the root object of File class to the list of files and directory from the external storage
//Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++) 
{
    // myList.add( list[i].getName() );
    File temp_file = new File(file.getAbsolutePath(),list[i].getName());
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
    if (temp_file.listFiles() != null) 
    {
        //Log.i("inside", "call fn");
        listfiles(temp_file);

    }
    else 
    {
        if (list[i].getName().toLowerCase().contains(".mp3"))
        {
            inFiles.add(list[i]);
        //Log.e("Music", list[i].getName());
        }
    }
}
like image 156
Pratik Avatar answered Oct 29 '22 16:10

Pratik