Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had 21 videos in the internal folder, by using videocursor.getcount() it was able to count all the videos.and showing correct path for file.
but when i place "for loop" to play the all the videos.but it was eable to play the all the videos.it was only playing last video.
And it was not playing in the loop.
Java


Java
package com.example.videolist;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoActivity extends Activity {
      private Cursor videocursor;
      private int video_column_index;
      ListView videolist;
      int count;
          String name = null;
       VideoView video_player_view;
    DisplayMetrics dm;
    SurfaceView sur_View;
    MediaController media_Controller;
    
       public class MediaFileInfo {

           private String fileName,filePath,fileType;

           public String getFileName() {
               return fileName;
           }

           public void setFileName(String fileName) {
               this.fileName = fileName;
           }

           public String getFilePath() {
               return filePath;
           }

           public void setFilePath(String filePath) {
               this.filePath = filePath;
           }

           public String getFileType() {
               return fileType;
           }

           public void setFileType(String fileType) {
               this.fileType = fileType;
           }
       }
       @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          System.gc();
       
            setContentView(R.layout.main);
            init_phone_video_grid();
      }

      private void init_phone_video_grid() {
            System.gc();
            String[] proj = { MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.SIZE };
                     videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null, null, null);

          
            count = videocursor.getCount();
            Log.d("count:", String.valueOf(count));
            videolist = (ListView) findViewById(R.id.VideoList);
            videolist.setAdapter(new VideoAdapter(getApplicationContext()));
          
            Log.d("No of Video",""+count);




         for(int i=0; i< count; i++) {
               Log.d("i", String.valueOf(i));
               MediaFileInfo mediaFileInfo =new MediaFileInfo();
          video_column_index =videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
          videocursor.moveToPosition(i);
            // Log.d("getcolumn", String.valueOf(videocursor.moveToPosition(i)));
               name = videocursor.getString(video_column_index);
               mediaFileInfo.setFileName(name);
               Log.d("name",name);
               int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
               Log.d("column_index", String.valueOf(column_index));
               videocursor.moveToPosition(i);

               String filepath = videocursor.getString(column_index);
             Log.d("last i", String.valueOf(i));
             Log.d("filepath",filepath);
               mediaFileInfo.setFilePath(filepath);
              video_player_view = (VideoView) findViewById(R.id.videoView);
              media_Controller = new MediaController(this);
              dm = new DisplayMetrics();
              this.getWindowManager().getDefaultDisplay().getMetrics(dm);
              int height = dm.heightPixels;
              int width = dm.widthPixels;
              video_player_view.setMinimumWidth(width);
              video_player_view.setMinimumHeight(height);

                  video_player_view.setMediaController(media_Controller);
              Log.d("last i", String.valueOf(i));
              Log.d("filepath",filepath);

         
                     for(i=0;i<videocursor.getCount();i++){
                         video_player_view.setVideoPath(filepath);
                    
                         video_player_view.requestFocus();
                         video_player_view.start();
            
             }

              video_player_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                  public void onCompletion(MediaPlayer player) {
                      Log.i("VideoView", "onCompletion()");
                      /
                      video_player_view.requestFocus();

                      video_player_view.start();
                  }

              });



          }

      }






      public class VideoAdapter extends BaseAdapter {
            private Context vContext;

                  public VideoAdapter(Context c) {
                        vContext = c;
            }

            public int getCount() {
                  return count;
            }

            public Object getItem(int position) {
                  return position;
            }

            public long getItemId(int position) {
                  return position;
            }
@Override
            public View getView(int position, View convertView, ViewGroup parent) {
                  System.gc();

                  TextView tv = new TextView(vContext.getApplicationContext());

                  String id = null;
                  if (convertView == null) {
                        video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                        videocursor.moveToPosition(position);
                       id = videocursor.getString(video_column_index);
                       

                       tv.setText(id);

                  } else
                        tv = (TextView) convertView;
                 return tv;
            }
      }

}


What I have tried:

tired while and do while loop no use.
Posted
Updated 14-Sep-16 2:47am

1 solution

comment out for the time being

setOnCompletionListener

This is a problem, and not sure what you are doing with

Java
for(i=0;i<videocursor.getcount();i++){>
   video_player_view.setVideoPath(filepath);


remove the second for loop {} around the path, requestfocus and start. You can even remove the setvideopath as you do that way above in the code. I must have been seeing things.

/Darren
 
Share this answer
 
v2
Comments
sumanth g 15-Sep-16 0:56am    
I had removed the loop but also it was not playing in videos one by one, it used to play the last 21th video.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900