soul6942
soul6942

Reputation: 67

Searching entire storage of phone and SD Card for Music Player

Back when I started to make this music player, just searching in the SD Card would work and show the music and the player would work properly. A couple weeks ago, I bought a new phone and decided to install the app onto it, but when I loaded the app, it couldn't find any music, so I need a way to change the current code so that it will search every folder of the phone, and the micro SD Card , for any file which ends in ".mp3".

Main Activity.java

class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
    return (name.endsWith(".mp3"));
    }
}

public class MainActivity extends ListActivity implements OnCompletionListener {
private static final String SD_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/");
private static final String PHONE_STORAGE = new String(Environment.getRootDirectory().getPath() + "/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private View play;
private View pause;
private View stop;
private View next;
private View prev;
private View replay;
private View shuffle;
private boolean isRepeat = false;
private boolean isShuffle = false;
public static int SONG_NUMBER = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updatePlaylist();
    play = (ImageButton) findViewById(R.id.imageButton1);
    pause = (ImageButton) findViewById(R.id.imageButton2);
    stop = (ImageButton) findViewById(R.id.imageButton3);
    next = (ImageButton) findViewById(R.id.imageButton4);
    prev = (ImageButton) findViewById(R.id.imageButton5);
    replay = (ImageButton) findViewById(R.id.imageButton6);
    shuffle = (ImageButton) findViewById(R.id.imageButton7);
    mp.setOnCompletionListener(this);
    play.setEnabled(false);
    pause.setEnabled(false);
    stop.setEnabled(false);
    next.setEnabled(false);
    prev.setEnabled(false);
    replay.setEnabled(false);
    shuffle.setEnabled(false);

}

@Override
protected void onListItemClick(ListView list, View view, int position,
        long id) {
    try {
        SONG_NUMBER = position;
        mp.reset();
        mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(position));
        mp.prepare();
        mp.start();
        play.setEnabled(false);
        pause.setEnabled(true);
        stop.setEnabled(true);
        next.setEnabled(true);
        prev.setEnabled(true);
        replay.setEnabled(true);
        shuffle.setEnabled(true);
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }
}

private void updatePlaylist() {
    File home = new File(SD_PATH);
    if (home.listFiles(new Mp3Filter()).length > 0) {
        for (File file : home.listFiles(new Mp3Filter())) {
            songs.add(file.getName());
        }
        ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                R.layout.song_item, songs);
        setListAdapter(songList);
    }
    File secondDirectory = new File(PHONE_STORAGE);
    if (secondDirectory.listFiles(new Mp3Filter()).length > 0) {
        for (File file : home.listFiles(new Mp3Filter())) {
            songs.add(file.getName());
        }
        ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                R.layout.song_item, songs);
        setListAdapter(songList);
    }
}

public void play(View view) {
    Toast.makeText(getApplicationContext(), "Playing song",
            Toast.LENGTH_SHORT).show();
    mp.start();
    play.setEnabled(false);
    pause.setEnabled(true);
    stop.setEnabled(true);
    next.setEnabled(true);
    prev.setEnabled(true);
    replay.setEnabled(true);
    shuffle.setEnabled(true);
}

public void pause(View view) {
    Toast.makeText(getApplicationContext(), "Pausing song",
            Toast.LENGTH_SHORT).show();
    mp.pause();
    play.setEnabled(true);
    pause.setEnabled(false);
    stop.setEnabled(true);
    next.setEnabled(true);
    prev.setEnabled(true);
    replay.setEnabled(false);
    shuffle.setEnabled(false);
}

public void stop(View view) {
    Toast.makeText(getApplicationContext(), "Stopping song",
            Toast.LENGTH_SHORT).show();
    mp.stop();
    play.setEnabled(false);
    pause.setEnabled(false);
    stop.setEnabled(false);
    next.setEnabled(false);
    prev.setEnabled(false);
    replay.setEnabled(false);
    shuffle.setEnabled(false);
    isRepeat = false;
    isShuffle = false;
}

public void next(View view) {
    isRepeat = false;
    try {
        if (isShuffle) {
            mp.reset();
            Random rand = new Random();
            SONG_NUMBER = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        }
        else {
        if (SONG_NUMBER < songs.size() - 1) {
            Toast.makeText(getApplicationContext(), "Next song",
                    Toast.LENGTH_SHORT).show();
            mp.reset();
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));

        } else {
            Toast.makeText(getApplicationContext(), "Please try again",
                    Toast.LENGTH_SHORT).show();
            SONG_NUMBER = -1;
        }
        mp.start();
        }
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }
}

public void prev(View view) {
    isRepeat = false;
    isShuffle = false;
    try {
        if (SONG_NUMBER == 0) {
            Toast.makeText(getApplicationContext(),
                    "Cannot play previous song", Toast.LENGTH_SHORT).show();
            prev.setEnabled(false);
        } else {
            Toast.makeText(getApplicationContext(), "Previous song",
                    Toast.LENGTH_SHORT).show();
            mp.reset();
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));
            SONG_NUMBER -= 2;
        }
        mp.start();
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    } catch (NullPointerException e) {
        Log.v(getString(R.string.prevButton), e.getMessage());
    }
}

public void replay(View view) {

    if (isRepeat) {
        Toast.makeText(getApplicationContext(), "Replaying All Songs",
                Toast.LENGTH_SHORT).show();
        isRepeat = false;
    } else {
        Toast.makeText(getApplicationContext(), "Replaying Current Song",
                Toast.LENGTH_SHORT).show();
        isRepeat = true;
        isShuffle = false;
    }

}

public void shuffle(View view) {
    if (isShuffle) {
        Toast.makeText(getApplicationContext(), "Playing Songs Normally",
                Toast.LENGTH_SHORT).show();
        isShuffle = false;
    } else {
        Toast.makeText(getApplicationContext(), "Shuffling Songs",
                Toast.LENGTH_SHORT).show();
        isShuffle = true;
        isRepeat = false;
    }
}

@Override
public void onCompletion(MediaPlayer mp1) {
    try {
        if (isRepeat) {
            mp.start();
        } else if (isShuffle) {
            mp.reset();
            Random rand = new Random();
            SONG_NUMBER = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
            mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        } else {
            mp.reset();
            SONG_NUMBER += 1;
            mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        }
        play.setEnabled(false);
        pause.setEnabled(true);
        stop.setEnabled(true);
        next.setEnabled(true);
        prev.setEnabled(true);
        replay.setEnabled(true);
        shuffle.setEnabled(true);
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }

}

public void onDestroy() {
    super.onDestroy();
    mp.release();
}
}

Upvotes: 0

Views: 113

Answers (1)

Arpit Khurana
Arpit Khurana

Reputation: 144

You don't need to search individual files in storages. MediaStore does it for you.Every audio file will be listed here

Updated

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

            String[] projection = {
                    MediaStore.Audio.Media.DATA,
                    };

            Cursor cursor = getActivity().managedQuery(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projection,
                    selection,
                    null,
                    null);

            while(cursor.moveToNext()){
            //do something with the path
            File f=new File(cursor.getString(0));
            System.out.println(cursor.getString(0));
            }

Upvotes: 1

Related Questions