java - Searching entire storage of phone and SD Card for Music Player -


back when started make music player, searching in sd card work , show music , player work properly. couple weeks ago, bought new phone , decided install app onto it, when loaded app, couldn't find music, need way change current code search every folder of phone, , micro sd card , file 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) {         (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) {         (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 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(); } } 

you don't need search individual files in storages. mediastore you.every audio file 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 path             file f=new file(cursor.getstring(0));             system.out.println(cursor.getstring(0));             } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -