java - The Surface Has Been Released Error when I try setDisplay to MediaPlayer -


i trying play video gallery .mp4 format , recorded video camera app. surface has been released error when try setdisplay. have read of forums , sure how handling surfaceview or holders. surface destroyed before call start , don't know how keep created. need communities advice first time this

i know gets path of file correctly , when remove attempts play video, audio part worked. maybe has fact see surface on homescreen when start app, xml may not correct think should okay:

    public class mainactivity extends activity implements onclicklistener,             onpreparedlistener, oncompletionlistener, surfaceholder.callback,             onbufferingupdatelistener, onvideosizechangedlistener {          private static final int select_video = 1;         private static final string tag = "mainactivity";         private byte[] videofile;         private int mvideowidth;         private int mvideoheight;         private mediaplayer mmediaplayer;         private surfaceview mpreview;         private surfaceholder holder;         private bundle extras;         private boolean misvideosizeknown = false;         private boolean misvideoreadytobeplayed = false;         private boolean hasactiveholder = false;          @suppresswarnings("deprecation")         @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);              mpreview = (surfaceview) findviewbyid(r.id.surface);             holder = mpreview.getholder();             holder.addcallback(this);             holder.settype(surfaceholder.surface_type_push_buffers);             extras = getintent().getextras();              setupbuttonclicklisteners();         }          private void setupbuttonclicklisteners() {             button exitbutton = (button) findviewbyid(r.id.exitbutton);             exitbutton.setonclicklistener(this);             button selectvideobutton = (button) findviewbyid(r.id.selectvideo);             selectvideobutton.setonclicklistener(this);         }          @override         public void onclick(view v) {             switch (v.getid()) {             case r.id.exitbutton:                 this.finish();                 break;             case r.id.selectvideo:                 intent intent = new intent();                 intent.settype("video/*");                 intent.setaction(intent.action_get_content);                 startactivityforresult(                         intent.createchooser(intent, "select video"), select_video);                 break;             }         }          public void onactivityresult(int requestcode, int resultcode, intent data) {             if (resultcode == result_ok) {                 switch (requestcode) {                 case select_video:                     docleanup();                     uri videouri = data.getdata();                     string path = getpath(videouri);                     boolean success = readfromfile(path);                     if (success) {                         // create new media player , set listeners                         playvideo(path);                     } else {                         //do nothing                     }                     log.i(tag, path);                     break;                 }             }         }          private void playvideo(string path) {             try {                 mmediaplayer = new mediaplayer();                 mmediaplayer.setdatasource(path);                 // here problem                  synchronized (this) {                     while (!hasactiveholder) {                         try {                             this.wait();                         } catch (interruptedexception e) {                           //print                         }                     }                 }                  mmediaplayer.setdisplay(holder);                 mmediaplayer.prepare();                 mmediaplayer.setonbufferingupdatelistener(this);                 mmediaplayer.setoncompletionlistener(this);                 mmediaplayer.setonpreparedlistener(this);                 mmediaplayer.setonvideosizechangedlistener(this);                 mmediaplayer.setaudiostreamtype(audiomanager.stream_music);              } catch (exception ex) {                 log.e(tag, "error: " + ex.getmessage(), ex);             }         }          private void docleanup() {             mvideowidth = 0;             mvideoheight = 0;             misvideoreadytobeplayed = false;             misvideosizeknown = false;         }          private boolean readfromfile(string path) {             file file = new file(path);             try {                 fileinputstream fis = new fileinputstream(file);                 log.i(tag, "file size: " + file.length());                 videofile = new byte[(int) file.length()];                 fis.read(videofile);                 fis.close();             } catch (exception ex) {                 ex.printstacktrace();                 return false;             }             return true;         }          @suppresswarnings("deprecation")         private string getpath(uri uri) {             string[] projection = { mediastore.video.media.data };             cursor cursor = managedquery(uri, projection, null, null, null);             int column_index = cursor.getcolumnindex(mediastore.video.media.data);             cursor.movetofirst();             return cursor.getstring(column_index);         }          @override         public void onprepared(mediaplayer mediaplayer) {             log.d(tag, "onprepared called");             misvideoreadytobeplayed = true;             if (misvideoreadytobeplayed && misvideosizeknown) {                 startvideoplayback();             }         }          @override         public void oncompletion(mediaplayer mp) {             log.d(tag, "oncompletion called");         }          @override         public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) {             log.d(tag, "surfacechanged called");          }          @override         public void surfacecreated(surfaceholder arg0) {             log.d(tag, "surfacecreated called");             synchronized (this) {                 hasactiveholder = true;                 this.notifyall();              }         }          @override         public void surfacedestroyed(surfaceholder arg0) {             log.d(tag, "surfacedestroyed called");             synchronized (this) {                 hasactiveholder = false;                 this.notifyall();             }         }          @override         public void onvideosizechanged(mediaplayer mp, int width, int height) {             log.v(tag, "onvideosizechanged called");             misvideosizeknown = true;             mvideowidth = width;             mvideoheight = height;             if (misvideoreadytobeplayed && misvideosizeknown) {                 startvideoplayback();             }         }          private void startvideoplayback() {             log.v(tag, "startvideoplayback");             holder.setfixedsize(mvideowidth, mvideoheight);             mmediaplayer.start();         }          @override         public void onbufferingupdate(mediaplayer arg0, int percent) {             log.d(tag, "onbufferingupdate percent:" + percent);          }          @override         protected void onpause() {             super.onpause();             releasemediaplayer();             docleanup();         }          @override         protected void ondestroy() {             super.ondestroy();             releasemediaplayer();             docleanup();         }          private void releasemediaplayer() {             if (mmediaplayer != null) {                 mmediaplayer.release();                 mmediaplayer = null;             }         }      } 

my xml surface below. once again, not sure why surface showing when app loads want video play there once selected gallery. :

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context=".mainactivity" >       <android.view.surfaceview            android:id="@+id/surface"           android:layout_width="fill_parent"           android:layout_height="fill_parent" />      <button         android:id="@+id/changefilepath"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:layout_margintop="78dp"         android:text="@string/changefilepath" />      <button         android:id="@+id/recordvideo"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/changefilepath"         android:layout_centerhorizontal="true"         android:text="@string/recordvideo" />      <button         android:id="@+id/exitbutton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/recordvideo"         android:layout_centerhorizontal="true"         android:layout_margintop="49dp"         android:text="@string/exitbutton" />      <button         android:id="@+id/selectvideo"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/recordvideo"         android:layout_centerhorizontal="true"         android:text="@string/selectvideo" />    </relativelayout> 


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -