i have implemented android media player clicking on songs text view display layout. layout displayed play pause .. , clicking on play stop works well. here thinking in users perspective implement below.
- clicking on songs text view should display songs in list view , in bottom of layout small controls should display play pause , stop
- it should play when other application opened.
to achieve point 2 used media player service , point 1, used `listview' not implemented. below code play song , and go till end. point can please me implement list view songs service?
public class songs extends activity { private mediaplayer mediaplayer; public textview songname, duration; private double timeelapsed = 0, finaltime = 0; private int forwardtime = 2000, backwardtime = 2000; private handler durationhandler = new handler(); private seekbar seekbar; private context context; private int getcurrentpos=0; private int[] songs={r.raw.sample_song,r.raw.sleepaway}; @override protected void oncreate(bundle savedinstancestate) { context=getapplicationcontext(); super.oncreate(savedinstancestate); setcontentview(r.layout.mediaplayer); initializeviews(); } public void initializeviews(){ songname = (textview) findviewbyid(r.id.songname); mediaplayer = mediaplayer.create(this, songs[getcurrentpos]); mediaplayer.setlooping(true); finaltime = mediaplayer.getduration(); duration = (textview) findviewbyid(r.id.songduration); seekbar = (seekbar) findviewbyid(r.id.seekbar); songname.settext("sample_song.mp3"); seekbar.setmax((int) finaltime); seekbar.setclickable(false); } // play mp3 song public void play(view view) { mediaplayer.start(); timeelapsed = mediaplayer.getcurrentposition(); seekbar.setprogress((int) timeelapsed); durationhandler.postdelayed(updateseekbartime, 100); } private runnable updateseekbartime = new runnable() { public void run() { //get current position timeelapsed = mediaplayer.getcurrentposition(); //set seekbar progress seekbar.setprogress((int) timeelapsed); //set time remaing double timeremaining = finaltime - timeelapsed; // duration.settext(string.format("%d min, %d sec", 12, timeunit.milliseconds.toseconds((long) timeremaining) - 12)); //repeat again in 100 miliseconds durationhandler.postdelayed(this, 100); } }; // pause mp3 song public void pause(view view) { mediaplayer.pause(); } // go forward @ forwardtime seconds public void forward(view view) { //check if can go forward @ forwardtime seconds before song endes if ((timeelapsed + forwardtime) <= finaltime) { timeelapsed = timeelapsed + forwardtime; //seek exact second of track mediaplayer.seekto((int) timeelapsed); } } // go backwards @ backwardtime seconds public void rewind(view view) { //check if can go @ backwardtime seconds after song starts if ((timeelapsed - backwardtime) > 0) { timeelapsed = timeelapsed - backwardtime; //seek exact second of track mediaplayer.seekto((int) timeelapsed); } } // go next song public void next(view view) throws ioexception{ try { mediaplayer.reset(); mediaplayer = mediaplayer.create(this, songs[getsongposition()]); mediaplayer.start(); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } } private int getsongposition(){ if(getcurrentpos==songs.length-1){ getcurrentpos=0; }else if(getcurrentpos==0){ getcurrentpos=getcurrentpos+1; }else{ getcurrentpos=getcurrentpos+1; } return getcurrentpos; } } layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="#333333" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" > <textview android:id="@+id/songname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="songname" /> <imageview android:id="@+id/mp3image" android:layout_width="match_parent" android:layout_height="200dp" android:padding="30dp" android:src="@drawable/music" android:background="#ffffff" android:layout_margin="30dp" /> <textview android:id="@+id/songduration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="songduration" /> <seekbar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="30dp" android:gravity="center_horizontal" android:orientation="horizontal" > <imagebutton android:id="@+id/media_rew" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:onclick="rewind" android:src="@android:drawable/ic_media_rew" /> <imagebutton android:id="@+id/media_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:onclick="pause" android:src="@android:drawable/ic_media_pause" /> <imagebutton android:id="@+id/media_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:onclick="play" android:src="@android:drawable/ic_media_play" /> <imagebutton android:id="@+id/media_ff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:onclick="forward" android:src="@android:drawable/ic_media_ff" /> <imagebutton android:id="@+id/media_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:onclick="next" android:src="@android:drawable/ic_media_next" /> </linearlayout> </linearlayout>
Comments
Post a Comment