audio - Android playing Music in Background -
i have activity code below:
public class player extends activity implements oncompletionlistener, onpreparedlistener, onerrorlistener, onbufferingupdatelistener, musicfocusable { private boolean playstate = false; private string station = "http://38.101.19.5:9157"; public static final float duck_volume = 0.1f; private string artistname = null; private string trackname = null; private textview artist; private textview track; private textview status; private button play; enum audiofocus { nofocusnoduck, // don't have audio focus, , can't duck nofocuscanduck, // don't have focus, can play @ low volume // ("ducking") focused // have full audio focus } private audiofocus maudiofocus = audiofocus.nofocusnoduck; private mediaplayer mplayer = null; private androidshoutcastlib shoutcast; private audiomanager maudiomanager; audiofocushelper maudiofocushelper = null; handler handler = new handler(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_player); maudiomanager = (audiomanager) getsystemservice(audio_service); // create audio focus helper, if audio focus feature // available (sdk 8 or above) if (android.os.build.version.sdk_int >= 8) { maudiofocushelper = new audiofocushelper(getapplicationcontext(), this); } else { maudiofocus = audiofocus.focused; // no focus feature, "have" audio focus } status = (textview) findviewbyid(r.id.status); artist = (textview) findviewbyid(r.id.artist); artist.setselected(true); track = (textview) findviewbyid(r.id.track); track.setselected(true); play = (button) findviewbyid(r.id.play); play.setonclicklistener(new onclicklistener() { @override public void onclick(view btn) { if (!playstate) { play.settext("pause"); handler.postdelayed(handleplayrequest, 300); } else { play.settext("play"); status.settext("press play"); handler.postdelayed(handleplayrequest, 300); } } }); shoutcast = new androidshoutcastlib(); try { shoutcast.setshoutcasturl(station); } catch (invalidstreamurlexception e) { // todo auto-generated catch block e.printstacktrace(); } shoutcast.setonmetadatachangedlistener(new metadatalistener(){ @override public void onmetadatachanged(metadata item) { artistname = item.artist; trackname = item.track; updatemeta(); } }); setvolumecontrolstream(audiomanager.stream_music); } public void ondestroy() { super.ondestroy(); shoutcast = null; handler.removecallbacks(handleplayrequest); } public void updatemeta() { handler.post(new runnable() { @override public void run() { // gets executed on ui thread can safely modify views artist.settext(artistname); track.settext(trackname); } }); } private final runnable handleplayrequest = new runnable() { public void run() { if (playstate) { log.d("player", "stop called"); giveupaudiofocus(); mplayer.stop(); mplayer.reset(); mplayer.release(); shoutcast.stopstream(); mplayer = null; playstate = false; } else { log.d("player", "play called"); createmediaplayer(); getaudiofocus(); try { mplayer.setdatasource(shoutcast.startstream()); } 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(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (invalidstreamurlexception e) { // todo auto-generated catch block e.printstacktrace(); } mplayer.prepareasync(); } } }; private void createmediaplayer() { mplayer = new mediaplayer(); // make sure media player acquire wake-lock while // playing. if don't // that, cpu might go sleep while song playing, // causing playback stop. // // remember use this, have declare // android.permission.wake_lock // permission in androidmanifest.xml. mplayer.setwakemode(getapplicationcontext(), powermanager.partial_wake_lock); // want media player notify when it's ready preparing, // , when it's done // playing: mplayer.setonpreparedlistener(this); mplayer.setoncompletionlistener(this); mplayer.setonerrorlistener(this); } private void startplayer() { mplayer.setvolume(1.0f, 1.0f); if (!mplayer.isplaying()) { log.d("player", "starting playback"); mplayer.start(); playstate = true; status.settext("streaming"); } } private void getaudiofocus() { if (maudiofocus != audiofocus.focused && maudiofocushelper != null && maudiofocushelper.requestfocus()) maudiofocus = audiofocus.focused; } private void giveupaudiofocus() { if (maudiofocus == audiofocus.focused && maudiofocushelper != null && maudiofocushelper.abandonfocus()) maudiofocus = audiofocus.nofocusnoduck; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_player, menu); return true; } @override public void onbufferingupdate(mediaplayer arg0, int arg1) { // todo auto-generated method stub } @override public boolean onerror(mediaplayer mp, int what, int extra) { playstate = false; handler.post(handleplayrequest); return false; } @override public void onprepared(mediaplayer arg0) { startplayer(); } @override public void oncompletion(mediaplayer arg0) { // todo auto-generated method stub } @override public void ongainedaudiofocus() { // todo auto-generated method stub } @override public void onlostaudiofocus(boolean canduck) { // todo auto-generated method stub } }
my requirement convert activity service class,i have tried not getting,because i'm new android new programing,
could 1 help?
if need play music in app, play in thread launched app/use asynctask class you.
use code. add class (enclosed in activity class).
public class backgroundsound extends asynctask<void, void, void> { @override protected void doinbackground(void... params) { mediaplayer player = mediaplayer.create(youractivity.this, r.raw.test_cbr); player.setlooping(true); // set looping player.setvolume(100,100); player.start(); return null; } }
on other hand, if want use service can @ this page.
Comments
Post a Comment