0

I've been searching everywhere for the solution, but it has come to a dead end. Please help!

I record and save the video as the follow:

File DirectoryFile = new File(VideoPath);
recorder.setOutputFile(DirectoryFile.getAbsolutePath());

I load all the videos and set the ListView adapter from the userPath as follow:

private File[] getNewImageFilesWithFilters() {
    File directory = new File(UserSavedDirectoryPATH);
    return directory.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase(Locale.getDefault()).endsWith(".mp4") 
                || name.toLowerCase(Locale.getDefault()).endsWith(".mkv");
        }
    });
}   

public void LoadListView() {
    for (File file : listFile){
        mVideoListViewObject = new VideoListViewObject();
        mVideoListViewObject.setName(file.getName());
        mVideoListViewObject.setVideoUrl(file.getAbsolutePath());
        VideoListViewObject_List.add(mVideoListViewObject); 
    }        
    mVideoListViewAdapter = new VideoListAdapter(this, VideoListViewObject_List);
    mListView.setAdapter(mVideoListViewAdapter); 
} 

The ListView Adapter:

public class VideoListAdapter extends BaseAdapter {
    private List<VideoListViewObject> VideoObjectList;
    private Context mContext;

    public VideoListAdapter(Context context, List<VideoListViewObject> newList){
        this.mContext = context;
        this.VideoObjectList = newList;
    }

    @Override
    public int getCount() {
        return VideoObjectList.size();
    }

    @Override
    public VideoListViewObject getItem(int position) {
        return VideoObjectList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {       
      ViewHolder viewHolder = new ViewHolder();

      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.listview_layout, parent, false);
          viewHolder.imageView = (ImageView)convertView.findViewById(R.id.ListViewImage); 
          viewHolder.layout = (RelativeLayout)convertView.findViewById(R.id.ListViewLayout);
          convertView.setTag(viewHolder);            
      } 
     else 
      {
         viewHolder = (ViewHolder)convertView.getTag();
      }

      Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(VideoObjectList.get(position).getVideoUrl(),Thumbnails.MICRO_KIND);    
      viewHolder.imageView.setImageBitmap(bmThumbnail);

The problem is the list is slow to load, especially when there are a lot of videos. This causes my VideoaAtivity to start very slow.

I love Piscasso and Universal Image Loader, but they only support images.

Does anyone know a better solution or a library that would help with the performance?

Thank you very much.

User2014
  • 87
  • 3
  • 9
  • The bottleneck is creating all of the thumbnails, you are creating the thumbnail every time getView is called. I have a similar project, but I have 15 to 20 videos that I create thumbnails for, I save the thumbnail bitmap with a class such as your (VideoListviewObject) since it is small in size for each video. The first time the list is loaded and you scroll through all of it's items, it is slow but then after that is very fast. – faljbour Apr 22 '15 at 23:57
  • Thanks for the comment, but it would be too slow at start. I am surprised you were the only one who can help with this issue, at least trying to help. – User2014 Apr 23 '15 at 16:27

1 Answers1

1

I just modified my own application to do similar logic to pre-create the thumbnails which made the list scroll very fast at the start, Add the thumbnail bitmap to the videoListViewObject and create the thumbnail when loading the video list. this way you do not have to create it every time getView is called in your adapter.

public class VideoListViewObject{

  private Bitmap bitmap = null;

............................

    public void setBitmap(Bitmap bitmap)
    {
      this.bitmap = bitmap;
    }
    public Bitmap getBitmap()
    {
      return this.bitmap;
    }

}


public void LoadListView() {
    for (File file : listFile){
        mVideoListViewObject = new VideoListViewObject();
        mVideoListViewObject.setName(file.getName());
        mVideoListViewObject.setVideoUrl(file.getAbsolutePath());
        Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(VideoObjectList.get(position).getVideoUrl(),Thumbnails.MICRO_KIND);    
        mVideoListViewObject.setBitmap(bmThumbnail);
        VideoListViewObject_List.add(mVideoListViewObject); 
    }        
    mVideoListViewAdapter = new VideoListAdapter(this, VideoListViewObject_List);
    mListView.setAdapter(mVideoListViewAdapter); 
} 

then change your BaseAdapter code to only create the thumbnail if it is null,

public class VideoListAdapter extends BaseAdapter {
    private List<VideoListViewObject> VideoObjectList;
    private Context mContext;

    public VideoListAdapter(Context context, List<VideoListViewObject> newList){
        this.mContext = context;
        this.VideoObjectList = newList;
    }

    @Override
    public int getCount() {
        return VideoObjectList.size();
    }

    @Override
    public VideoListViewObject getItem(int position) {
        return VideoObjectList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {       
      ViewHolder viewHolder = new ViewHolder();

      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.listview_layout, parent, false);
          viewHolder.imageView = (ImageView)convertView.findViewById(R.id.ListViewImage); 
          viewHolder.layout = (RelativeLayout)convertView.findViewById(R.id.ListViewLayout);
          convertView.setTag(viewHolder);            
      } 
     else 
      {
         viewHolder = (ViewHolder)convertView.getTag();
      }

      VideoListViewObject  mVideoListViewObject = getItem(position);
      Bitmap  bmThumbnail = mVideoListViewObject.getBitmap();
      if(bmThumbnail==null)
      {
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(VideoObjectList.get(position).getVideoUrl(),Thumbnails.MICRO_KIND);    
      }
      viewHolder.imageView.setImageBitmap(bmThumbnail);
faljbour
  • 1,367
  • 2
  • 8
  • 15
  • it's better to use this Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), Thumbnails.MICRO_KIND); instead of Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(VideoObjectList.get(position).getVideoUrl(),Thumbnails.MICRO_KIND); – User2014 Apr 23 '15 at 22:54
  • This is definitely much better than what I had. However, the app opens kinda slow though. Since your solution is a lot better than mine, I will just accept your answer unless someone else knows better solution and willing to help. Many thanks for your help Faljbour – User2014 Apr 23 '15 at 22:57
  • Yes, that is what I noticed and expected. My app has the exact same issue. Populating the list will take longer now. What I recommend is to load your video list in AsyncTask that runs in the background and display some kind of status to the user that the app is initializing. – faljbour Apr 23 '15 at 23:14