Skip to content Skip to sidebar Skip to footer

Android List Music By Folders And Play Them

I'm developing music player for android that is already on the market. Users are asking to add a Folder view to list all folders that has music in their smartphone and I want to de

Solution 1:

One possible solution as I said is to start scanning all directories from the SDCard with this FileFilter that I wrote:

This AudioFilter is applied to a directory that returns all files that is a song (checking extensions) and all directories that contains a song and not contains a .nomedia

This solution, anyway, need to be skipped because it takes too long to load the dir list (because of the recursive alg)

package com.designfuture.music.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;

import com.designfuture.framework.util.LogHelper;

publicclassAudioFileFilterimplementsFileFilter{

    protected static final String TAG = "AudioFileFilter";
    /**
     * allows Directories
     */privatefinal boolean allowDirectories;

    public AudioFileFilter( boolean allowDirectories) {
        this.allowDirectories = allowDirectories;
    }

    public AudioFileFilter() {
        this(true);
    }

    @Overridepublic boolean accept(File f) {
        if ( f.isHidden() || !f.canRead() ) {
            returnfalse;
        }

        if ( f.isDirectory() ) {
            return checkDirectory( f );
        }
        return checkFileExtension( f );
    }

    private boolean checkFileExtension( File f ) {
        String ext = getFileExtension(f);
        if ( ext == null) returnfalse;
        try {
            if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) {
                returntrue;
            }
        } catch(IllegalArgumentException e) {
            //Not known enum valuereturnfalse;    
        }
        returnfalse; 
    }

    private boolean checkDirectory( File dir ) {
        if ( !allowDirectories ) {
            returnfalse;
        } else {
            final ArrayList<File> subDirs = new ArrayList<File>();
            int songNumb = dir.listFiles( new FileFilter() {

                @Overridepublic boolean accept(File file) {
                    if ( file.isFile() ) {
                        if ( file.getName().equals( ".nomedia" ) )
                            returnfalse;

                        return checkFileExtension( file );
                    } elseif ( file.isDirectory() ){
                        subDirs.add( file );
                        returnfalse;
                    } elsereturnfalse;
                }
            } ).length;

            if ( songNumb > 0 ) {
                LogHelper.i(TAG, "checkDirectory: dir " + dir.toString() + " return true con songNumb -> " + songNumb );
                returntrue;
            }

            for( File subDir: subDirs ) {
                if ( checkDirectory( subDir ) ) {
                    LogHelper.i(TAG, "checkDirectory [for]: subDir " + subDir.toString() + " return true" );
                    returntrue;
                }
            }
            returnfalse;
        }       
    }

    private boolean checkFileExtension( String fileName ) {
        String ext = getFileExtension(fileName);
        if ( ext == null) returnfalse;
        try {
            if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) {
                returntrue;
            }
        } catch(IllegalArgumentException e) {
            //Not known enum valuereturnfalse;    
        }
        returnfalse; 
    }

    public String getFileExtension( File f ) {
        return getFileExtension( f.getName() );
    }

    public String getFileExtension( String fileName ) {
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            return fileName.substring(i+1);
        } elsereturnnull;
    }

    /**
     * Files formats currently supported by Library
     */publicenum SupportedFileFormat
    {
        _3GP("3gp"),
        MP4("mp4"),
        M4A("m4a"),
        AAC("aac"),
        TS("ts"),
        FLAC("flac"),
        MP3("mp3"),
        MID("mid"),
        XMF("xmf"),
        MXMF("mxmf"),
        RTTTL("rtttl"),
        RTX("rtx"),
        OTA("ota"),
        IMY("imy"),
        OGG("ogg"),
        MKV("mkv"),
        WAV("wav");

        private String filesuffix;

        SupportedFileFormat( String filesuffix ) {
            this.filesuffix = filesuffix;
        }

        public String getFilesuffix() {
            return filesuffix;
        }
    }

}

Solution 2:

MediaStore already know which are the correct paths to follow (because it needs to know them to re-scan every times the sdcard) so I was wondering if there is a way to get those paths and use them to show music to the user.

MediaStore scans all of external storage, AFAIK, not just some specific root folders.

Post a Comment for "Android List Music By Folders And Play Them"