android - How to add list of songs from storage and display it on one of my fragments -
how add list of songs inside 1 of fragment java , xml , mainactivity java , xml.
this other java classes related adding songs
1- song.java
public class song { private long id; private string title; private string artist; private long duration; public song(long songid, string songtitle, string songartist) { id=songid; title=songtitle; artist=songartist; } public long getid() { return id; } public string gettitle() { return title; } public string getartist() { return artist; } }
2- musicservice.java
public class musicservice extends service implements mediaplayer.onpreparedlistener, mediaplayer.onerrorlistener, mediaplayer.oncompletionlistener { ////////////////////////////////// declarations //////////////////////////////////////////////////// //media player private mediaplayer player; //song list private arraylist<song> songs; //current position private int songposn; //binder private final ibinder musicbind = new musicbinder(); //title of current song private string songtitle=""; //notification id private static final int notify_id=1; //shuffle flag , random private boolean shuffle=false; private random rand; public void oncreate(){ //create service super.oncreate(); //initialize position songposn=0; //random rand=new random(); //create player player = new mediaplayer(); //initialize initmusicplayer(); } public void initmusicplayer(){ //set player properties player.setwakemode(getapplicationcontext(), powermanager.partial_wake_lock); player.setaudiostreamtype(audiomanager.stream_music); //set listeners player.setonpreparedlistener(this); player.setoncompletionlistener(this); player.setonerrorlistener(this); } //pass song list public void setlist(arraylist<song> thesongs){ songs = thesongs; } //binder public class musicbinder extends binder { musicservice getservice() { return musicservice.this; } } //activity bind service @override public ibinder onbind(intent intent) { return musicbind; } //release resources when unbind @override public boolean onunbind(intent intent){ player.stop(); player.release(); return false; } //////////////////////////////////////// play song /////////////////////////////////////////// public void playsong(){ //play player.reset(); //get song song playsong = songs.get(songposn); //get title songtitle = playsong.gettitle(); //get id long currsong = playsong.getid(); //set uri uri trackuri = contenturis.withappendedid( android.provider.mediastore.audio.media.external_content_uri, currsong); //set data source try{ player.setdatasource(getapplicationcontext(), trackuri); } catch(exception e){ log.e("music service", "error setting data source", e); } player.prepareasync(); } //////////////////////////////////////////////////////////////////////////////////////////////// //set song public void setsong(int songindex){ songposn=songindex; } ///////////////////////////completion,prepared,error//////////////////////////////////////////// @override public void oncompletion(mediaplayer mp) { //check if playback has reached end of track if(player.getcurrentposition()>0){ mp.reset(); playnext(); } } @override public boolean onerror(mediaplayer mp, int what, int extra) { log.v("music player", "playback error"); mp.reset(); return false; } @override public void onprepared(mediaplayer mp) { //start playback mp.start(); //notification intent notintent = new intent(this, mainactivity.class); notintent.addflags(intent.flag_activity_clear_top); pendingintent pendint = pendingintent.getactivity(this, 0, notintent, pendingintent.flag_update_current); notification.builder builder = new notification.builder(this); builder.setcontentintent(pendint) .setsmallicon(r.drawable.play) .setticker(songtitle) .setongoing(true) .setcontenttitle("playing") .setcontenttext(songtitle); notification not = builder.build(); startforeground(notify_id, not); } //////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////playback methods/////////////////////////////////////////////// public int getposn(){ return player.getcurrentposition(); } public int getdur(){ return player.getduration(); } public boolean ispng(){ return player.isplaying(); } public void pauseplayer(){ player.pause(); } public void seek(int posn){ player.seekto(posn); } public void go(){ player.start(); } //skip previous track public void playprev(){ songposn--; if(songposn<0) songposn=songs.size()-1; playsong(); } //skip next public void playnext(){ if(shuffle){ int newsong = songposn; while(newsong==songposn){ newsong=rand.nextint(songs.size()); } songposn=newsong; } else{ songposn++; if(songposn>=songs.size()) songposn=0; } playsong(); } //toggle shuffle public void setshuffle(){ if(shuffle) shuffle=false; else shuffle=true; } @override public void ondestroy() { stopforeground(true); } }
3- songadapter.java
public class songadapter extends baseadapter { //song list , layout private arraylist<song> songs; private layoutinflater songinf; //constructor public songadapter(context c, arraylist<song> thesongs){ songs = thesongs; songinf=layoutinflater.from(c); } @override public int getcount() { return songs.size(); } @override public song getitem(int arg0) { return null; } @override public long getitemid(int arg0) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { //map song layout linearlayout songlay = (linearlayout)songinf.inflate(r.layout.song, parent, false); //get title , artist views textview songview = (textview)songlay.findviewbyid(r.id.song_title); textview artistview = (textview)songlay.findviewbyid(r.id.song_artist); //get song using position song currsong = songs.get(position); //get title , artist strings songview.settext(currsong.gettitle()); artistview.settext(currsong.getartist()); //set position tag songlay.settag(position); return songlay; } }
you have adapter need have listview handle data.
- first, please include listview widget in layout of
fragment
- second, set adapter
listview
your adapter should use viewholder
improve performance
Comments
Post a Comment