android - VideoView inside Fragment -


edit: thought had problem implementation of fragment caused videoview not work, turned out implementation correct , had problem layout xml of videoview. ill keep code example of correct implementation of videoview inside fragment using viewpager:)

this fragment:

import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.media.mediaplayer; import android.media.thumbnailutils; import android.net.uri; import android.os.bundle;  import android.provider.mediastore; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.mediacontroller; import android.widget.textview; import android.widget.videoview;  import uk.co.senab.photoview.photoview; import uk.co.senab.photoview.photoviewattacher;   public class screenslidepagefragment extends fragment {      private context context = null; //todo make setter     private string filename;     private final string file_name = "file_name";      /**      * factory method fragment class. constructs new fragment.      */     public static screenslidepagefragment create(string filename, context context) {         screenslidepagefragment fragment = new screenslidepagefragment();         fragment.setfilename(filename);         fragment.setcontext(context);         return fragment;     }      public screenslidepagefragment() {      }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate); //        filename = getarguments().getstring(file_name);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout containing title , body text.         viewgroup rootview = (viewgroup) inflater                 .inflate(r.layout.fragment_screen_slide_page, container, false);          // set title view show page number.         string name = this.filename;         if (name != null) {             if (name.endswith(".jpg")) {                 bitmap mybitmap = bitmapfactory.decodefile(name);                 imageview view = (imageview) rootview.findviewbyid(r.id.imageview);                 view.setimagebitmap(mybitmap);                 photoviewattacher mattacher = new photoviewattacher(view);             }             if (name.endswith(".mp4")) {                 final videoview video = (videoview) rootview.findviewbyid(r.id.videoview);                  video.setonpreparedlistener(new mediaplayer.onpreparedlistener() {                     @override                     public void onprepared(mediaplayer mp) {                         video.requestfocus();                         video.start();                     }                 });                 video.setvideouri(uri.parse(name)); //                bitmap bitmap = thumbnailutils.createvideothumbnail("/storage/sdcard0/android/data/huji.ac.il.test/files/savedfiles/150401015447.mp4", mediastore.video.thumbnails.micro_kind); //                imageview view = (imageview) rootview.findviewbyid(r.id.imageview); //                view.setimagebitmap(bitmap);             }          }         return rootview;     }      public void setfilename(string filename) {         this.filename = filename;     }      public void setcontext(context context) {         this.context = context;     } } 

this layout "fragment_screen_slide_page.xml":

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     android:layout_gravity="center"      <imageview         android:id="@+id/imageview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_margin="0dp"          />      <videoview         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:id="@+id/videoview"         android:layout_gravity="center_horizontal" />   </linearlayout> 

here activity usess fragment:

public class screenslideactivity extends fragmentactivity {       private viewpager mpager;     private pageradapter mpageradapter;     private arraylist<string> filenamelist;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         view decorview = getwindow().getdecorview();         // hide status bar.         int uioptions = view.system_ui_flag_fullscreen;         decorview.setsystemuivisibility(uioptions);          setcontentview(r.layout.activity_screen_slide);          filenamelist=getimagesfromstorage();         // instantiate viewpager , pageradapter.         mpager = (viewpager) findviewbyid(r.id.pager);         mpageradapter = new screenslidepageradapter(getsupportfragmentmanager(), filenamelist,this);         mpager.setadapter(mpageradapter);      }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {          int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      public arraylist<string> getimagesfromstorage()     {         arraylist<string> filenamelist= new arraylist<string>();         file file= new file(getapplicationcontext().getexternalfilesdir(null).getabsolutepath()+ file.separator + "savedfiles");          if (file.isdirectory())         {             file[] listoffiles= file.listfiles();             (int = 0; <listoffiles.length; i++)             {                  filenamelist.add(listoffiles[i].getabsolutepath());             }         }         return filenamelist;     }  } 

i tried turning on gpu acceleration post suggested didn't work. tried playing video on main activity instead on fragment , worked, im gussing problem fragment. suggestions?

edit:

the adapter:

public class screenslidepageradapter extends fragmentstatepageradapter {     private arraylist<string> filenames;     private context context=null;       public screenslidepageradapter(fragmentmanager fragmentmanager, arraylist<string> filenamelist,context context) {         super(fragmentmanager);         this.filenames=filenamelist;         this.context=context;     }       @override     public fragment getitem(int position) {          return screenslidepagefragment.create(filenames.get(position),this.context);     }      @override     public int getcount() {         return filenames.size();     } } 

you need make sure have set:

<uses-permission android:name="android.permission.read_external_storage" /> 

in androidmanifest.xml

also, think you're not starting video @

in videoview initialisation can access mediaplayer within videoview using:

videoview video = (videoview) rootview.findviewbyid(r.id.videoview); video.setonpreparedlistener(new mediaplayer.onpreparedlistener() {                 @override                 public void onprepared(mediaplayer mp) {                     videoview.requestfocus();                     videoview.start();                 }   }); video.setvideouri(uri.parse(pathforthefile)); 

edit: local video your'e trying play, don't need wait mediaplayer prepared, should work:

videoview video = (videoview) rootview.findviewbyid(r.id.videoview);                video.setvideouri(uri.parse(pathforthefile)); video.start(); video.requestfocus(); 

so, wont need these lines:

mediacontroller mc=  new mediacontroller(getactivity());                 mc.setanchorview(video);                 video.setmediacontroller(mc);                 video.requestfocus(); 

let me know if worked


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -