when i changed orientation portrait to landscape i want layout should change for landscape and video is start from same position in android? -
this whole code media player problem when changed orientation video playing beginning should do.i add "android:configchanges="orientation|screensize|keyboardhidden" in menifest. please help
//mediaplayeractivity.java package com.focusmedica.ud_en_acne; import android.app.activity; import android.content.context; import android.content.intent; import android.content.res.configuration; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.matrix; import android.graphics.paint; import android.graphics.path; import android.media.mediametadataretriever; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.media.thumbnailutils; import android.net.uri; import android.os.bundle; import android.os.environment; import android.os.handler; import android.util.log; import android.view.motionevent; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.imageview; import android.widget.linearlayout; import android.widget.relativelayout; import android.widget.seekbar; import android.widget.textview; import android.widget.videoview; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arraylist; public class mediaplayeractivity extends activity implements seekbar.onseekbarchangelistener { imageview pauseandresume,refresh,next,prev,screenshot,pencil; seekbar videoprogress; private handler mhandler = new handler();; private utilities util; textview current; boolean playflag,istrue=false; videoview videoview; int vduration; linearlayout linear; relativelayout relate; view mview; private paint mpaint; private canvas mcanvas; /*static int[] video_url=new int[]{ r.raw.introduction,r.raw.causes,r.raw.risk_factors,r.raw.types_of_acne, r.raw.signs_and_symptoms,r.raw.treatment,r.raw.prevention_and_self_care, };*/ int video_url=r.raw.introduction; string path = environment.getexternalstoragedirectory() + file.separator + "pictures/screenshot.png"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_play); //setrequestedorientation(activityinfo.screen_orientation_locked); videoview=(videoview)findviewbyid(r.id.videoview); videoprogress=(seekbar)findviewbyid(r.id.seekbar); videoprogress.setonseekbarchangelistener(this); pauseandresume=(imageview) findviewbyid(r.id.btnpauseresume); relate=(relativelayout) findviewbyid(r.id.relate); linear=(linearlayout)findviewbyid(r.id.linear); screenshot=(imageview)findviewbyid(r.id.btnscreenshot); refresh=(imageview)findviewbyid(r.id.btnrefresh); pencil=(imageview)findviewbyid(r.id.btnpencil); current=(textview)findviewbyid(r.id.current); linear.setvisibility(view.invisible); util = new utilities(); bundle extras = getintent().getextras(); final int position = extras.getint("position"); final uri uri=uri.parse("android.resource://" + getpackagename() + "/"+ video_url); mediametadataretriever retriever = new mediametadataretriever(); retriever.setdatasource(this, uri.parse("android.resource://" + getpackagename() + "/"+ video_url)); string time = retriever.extractmetadata(mediametadataretriever.metadata_key_duration); vduration = integer.parseint(time ); relate.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (linear.getvisibility() == view.invisible) { new handler().postdelayed(new runnable() { @override public void run() { linear.setvisibility(view.visible); } }, 100); }else{ linear.setvisibility(view.invisible); } } }); refresh.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (istrue == true) { relate.removeview(mview); mview = new drawingview(mediaplayeractivity.this); relate.addview(mview); } } }); pencil.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(istrue==false) { mview = new drawingview(mediaplayeractivity.this); relate.addview(mview, new viewgroup.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent)); init(); istrue=true; }else if(istrue==true){ relate.removeview(mview); istrue=false; } } }); pauseandresume.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (pauseandresume.getdrawable().getconstantstate()== getresources().getdrawable(r.drawable.pause).getconstantstate()) { videoview.pause(); pauseandresume.setimageresource(r.drawable.play1); } else { videoview.start(); pauseandresume.setimageresource(r.drawable.pause); } } }); screenshot.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { bitmap bitmap = takescreenshot(mediaplayeractivity.this,uri); savebitmap(bitmap); } }); playvideo(uri); } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { //toast.maketext(getapplicationcontext(),"seekbar progress: "+progress, toast.length_short).show(); } @override public void onstarttrackingtouch(seekbar seekbar) { mhandler.removecallbacks(mupdatetimetask); } @override public void onstoptrackingtouch(seekbar seekbar) { mhandler.removecallbacks(mupdatetimetask); int totalduration =vduration; int currentposition = util.progresstotimer(seekbar.getprogress(), totalduration); // forward or backward seconds videoview.seekto(currentposition); // update timer progress again updateprogressbar(); } //canvas private void init() { mpaint = new paint(); mpaint.setdither(true); mpaint.setcolor(color.red); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(3); } private bitmap takescreenshot(context context, uri uri) { mediametadataretriever mediametadataretriever = new mediametadataretriever(); try { mediametadataretriever.setdatasource(context, uri); bitmap bitmap = mediametadataretriever.getframeattime(videoview.getcurrentposition()*1000); bitmap mainbitmap= thumbnailutils.extractthumbnail(bitmap, videoview.getwidth(), videoview.getheight(), 2); bitmap bmoverlay = bitmap.createbitmap(videoview.getwidth(), videoview.getheight(), bitmap.getconfig()); relate.setdrawingcacheenabled(true); bitmap bitmap1=relate.getdrawingcache(); mcanvas=new canvas(bmoverlay); mcanvas.drawbitmap(mainbitmap,new matrix(), null); mcanvas.drawbitmap(bitmap1, 0,0, null); if(null != mainbitmap) { return bmoverlay; } return bmoverlay; } catch (throwable t) { return null; } { try { mediametadataretriever.release(); } catch(runtimeexception e) { } } } private void savebitmap(bitmap bitmap) { fileoutputstream fos; try { fos =new fileoutputstream(path); bitmap.compress(bitmap.compressformat.png, 100, fos); fos.flush(); fos.close(); sentmail(path); } catch (filenotfoundexception e) { log.e("grec", e.getmessage(), e); } catch (ioexception e) { log.e("grec", e.getmessage(), e); } } private void sentmail(string path) { intent emailintent = new intent(android.content.intent.action_send); emailintent.putextra(android.content.intent.extra_email, new string[]{"p.ashish029@gmail.com"}); emailintent.putextra(android.content.intent.extra_subject, "acne"); emailintent.putextra(android.content.intent.extra_text, "screenshot of image"); emailintent.settype("image/jpg"); uri myuri = uri.parse("file://" + path); emailintent.putextra(intent.extra_stream, myuri); startactivity(intent.createchooser(emailintent, "send mail...")); } public void updateprogressbar() { mhandler.postdelayed(mupdatetimetask, 100); } private runnable mupdatetimetask = new runnable() { public void run() { long totalduration = vduration; long currentduration = videoview.getcurrentposition(); // displaying total duration time //total.settext(""+util.millisecondstotimer(totalduration)); // displaying time completed playing current.settext(""+util.millisecondstotimer(currentduration)); // updating progress bar int progress = (int)(util.getprogresspercentage(currentduration, totalduration)); //log.d("progress", ""+progress); videoprogress.setprogress(progress); // running thread after 100 milliseconds mhandler.postdelayed(this, 100); } }; private void playvideo(uri uri) { if (videoview.isplaying() ) { videoview.stopplayback(); } videoview.setvideouri(uri); videoview.start(); updateprogressbar(); videoview.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { playflag=false; finish(); } }); } //canvas private class drawingview extends view { private path path; private bitmap mbitmap; public drawingview(context context) { super(context); path = new path(); mbitmap = bitmap.createbitmap(relate.getwidth(), relate.getheight(), bitmap.config.argb_8888); canvas mcanvas = new canvas(mbitmap); } private arraylist<pathwithpaint> _graphics1 = new arraylist<pathwithpaint>(); @override public boolean ontouchevent(motionevent event) { pathwithpaint pp = new pathwithpaint(); if (event.getaction() == motionevent.action_down) { path.moveto(event.getx(), event.gety()); path.lineto(event.getx(), event.gety()); } else if (event.getaction() == motionevent.action_move) { path.lineto(event.getx(), event.gety()); pp.setpath(path); pp.setmpaint(mpaint); _graphics1.add(pp); } invalidate(); return true; } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (_graphics1.size() > 0) { canvas.drawpath( _graphics1.get(_graphics1.size() - 1).getpath(), _graphics1.get(_graphics1.size() - 1).getmpaint()); } } } @override public void onconfigurationchanged(configuration newconfig) { // todo auto-generated method stub super.onconfigurationchanged(newconfig); setcontentview(r.layout.activity_play); oncreate(new bundle()); } }
android provides way store current states , retrieve them when orientation changes.
override onsaveinstancestate(bundle savedinstancestate)
method in activity , save state of video playing,
@override public void onsaveinstancestate(bundle savedinstancestate) { // save current video progress savedinstancestate.putlong("videoprogress", videoview.getprogress()); super.onsaveinstancestate(savedinstancestate); }
and in oncreate()
method can retrieve video progress as,
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate);first if (savedinstancestate != null) { currentprogress = savedinstancestate.getlong("videoprogress"); videoview.setprogress(currentprogress); } ... }
Comments
Post a Comment