Android MediaPlayer to play video stream in Service screen rotation -
i create activity video player play online stream using mediaplayer class , surfaceview display. i'm creating mediaplayer in separate service after screen rotation player don't have created again , don't have connect stream. problem don't know how write activity service wouldn't start every time after screen rotation.
my code below, in onstart() start service don't know how change didn't start every time.
public class videoplayeractivity extends activity implements surfaceholder.callback { private string path; private surfaceholder vidholder; private surfaceview vidsurface; private videoservice videoservice; private intent playintent; private boolean videobound = false; private serviceconnection musicconnection = new serviceconnection() { @override public void onserviceconnected(componentname name, ibinder service) { videoservice.videobinder binder = (videoservice.videobinder) service; videoservice = binder.getservice(); videoservice.seturl(path); videobound = true; if (vidholder != null && videoservice.getmediaplayer() != null) { videoservice.getmediaplayer().setdisplay(vidholder); videoservice.playvideo(); } } @override public void onservicedisconnected(componentname name) { videobound = false; } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_player); path = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"; //todo tmp playintent = new intent(this, videoservice.class); bindservice(playintent, musicconnection, context.bind_auto_create); startservice(playintent); vidsurface = (surfaceview) findviewbyid(r.id.surfview); vidholder = vidsurface.getholder(); vidholder.addcallback(videoplayeractivity.this); } @override public void surfacecreated(surfaceholder surfaceholder) { if (videoservice != null && videoservice.getmediaplayer() != null) { videoservice.getmediaplayer().setdisplay(vidholder); } } @override public void surfacechanged(surfaceholder surfaceholder, int i, int i1, int i2) { log.d("serviceconnection", "surfacechanged " + + " " + i1 + " " + i2); } @override public void surfacedestroyed(surfaceholder surfaceholder) { log.d("serviceconnection", "surfacedestroyed"); } @override protected void ondestroy() { super.ondestroy(); unbindservice(musicconnection); stopservice(playintent); videoservice = null; } }
service class:
public class videoservice extends service implements onpreparedlistener { private mediaplayer player; private string path; private final ibinder musicbind = new videobinder(); @override public void oncreate() { log.d("videoservice", "oncreate"); super.oncreate(); player = new mediaplayer(); initmusicplayer(); } @override public ibinder onbind(intent intent) { return musicbind; } @override public boolean onunbind(intent intent){ player.stop(); player.release(); return false; } public void initmusicplayer() { player.setwakemode(getapplicationcontext(), powermanager.partial_wake_lock); player.setaudiostreamtype(audiomanager.stream_music); player.setonpreparedlistener(this); } public void playvideo() { try { player.setdatasource(path); player.prepareasync(); } catch (ioexception e) {} } @override public void onprepared(mediaplayer mediaplayer) { mediaplayer.start(); } public void seturl(string url) { path = url; } public mediaplayer getmediaplayer() { return player; } public class videobinder extends binder { public videoservice getservice() { return videoservice.this; } } }
you either check savedinstancestate
being null start service if activity freshly created
if (savedinstancestate == null) { startservice... }
or handle screen rotation adding
android:configchanges="orientation|keyboardhidden|screensize"
to activity in manifest. oncreate not called on rotation more.
Comments
Post a Comment