Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i had a code with scans the external device and get the list of videos. we need to select the video to play.
i need to automate the player so it can play all the videos one by one.
how can i do that.
Java


Java
package com.list.videolist;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.VideoView;

public class VideoActivity extends Activity {
      private Cursor videocursor;
      private int video_column_index;
      ListView videolist;
      int count;
     //private  VideoView mVideoView;
     //private MediaController controller;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          System.gc();
        //  mVideoView = (VideoView) findViewById(R.id.videoView);
         // mVideoView = new VideoView(getApplicationContext());
          //setContentView(mVideoView);

         //controller = new MediaController(this);
         // controller.setAnchorView(mVideoView);
         // controller.setMediaPlayer(mVideoView);
            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 };
          //alternativie to managedQuery.
           videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null, null, null);

          //videocursor = managedQuery(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()));
            videolist.setOnItemClickListener(videogridlistener);
      }

      private OnItemClickListener videogridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                  System.gc();

                  video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                  videocursor.moveToPosition(position);
                  String filename = videocursor.getString(video_column_index);

                  Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
                  intent.putExtra("videofilename", filename);


                  startActivity(intent);
                  Log.d("Video Path : ",filename);
              //  mVideoView.setVideoPath(filename);
              //  mVideoView.setMediaController(controller);
              //  mVideoView.requestFocus();

              //  mVideoView.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);
                       // video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                      //  videocursor.moveToPosition(position);
                      //  id += " Size(KB):" + videocursor.getString(video_column_index);

                      //ImageView iv = new ImageView(vContext);
                      //ContentResolver crThumb = getContentResolver();
                      //BitmapFactory.Options options=new BitmapFactory.Options();
                      //options.inSampleSize = 1;
                      //Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, position, MediaStore.Video.Thumbnails.MICRO_KIND, options);
                      //iv.setImageBitmap(curThumb);

                       tv.setText(id);

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

}


This MainActivity calls the videoactivity.
Java



Java
package com.list.videolist;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class ViewVideo extends Activity {
    private String filename;
    //12/9/2016 -start
   // int count 0;
   // String _path;
    //12/9/2016 -end
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // FrameLayout frame = new FrameLayout(this);
        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        filename = extras.getString("videofilename");
        // VideoView vv = new VideoView(getApplicationContext());

        final VideoView vv = new VideoView(this);
        // final VideoView vv = (VideoView) findViewById(R.id.videoView);
        //12/9/2016-start
       // _path = "/mnt/media_rw/usbdisk/"+count+".*";
        //vv.setVideoPath(_path);
        //vv.start();
        //12/9/16-end

        setContentView(vv);
        //getWindow().setLayout(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
        vv.setVideoPath(filename);
        vv.setMediaController(new MediaController(this));
        android.widget.FrameLayout.LayoutParams vvParams = (android.widget.FrameLayout.LayoutParams) vv.getLayoutParams();
        vvParams.gravity = 17; //gravity 17 = center horizontal & vertical
        vvParams.width = 5000;
        vv.setLayoutParams(vvParams);
        //setContentView(frame, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        vv.requestFocus();
      //  Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/udisk/videos");
      //  vv.setVideoURI(uri);
        vv.start();


        // vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        //    @Override
        //    public void onPrepared(MediaPlayer mediaPlayer) {
        //        mediaPlayer.setLooping(true);
        //     }
        //  });

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



               //for loogping vv.start();
                // finish() for closing.
                finish();
                vv.requestFocus();


            }

        });

    }
}


What I have tried:

Searching how to automate the process
Posted
Updated 12-Sep-16 22:39pm

1 solution

Not sure your design is correct, it looks like you present the user with a list they click and you play. However you only pass the single filename to the video player. If you had the full list you could via completion just keep loading the next file and playing it.

Java
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer player) {
            Log.i("VideoView", "onCompletion()");

            vv.setVideoPath(next_filename); // the next file in the list 

            vv.start();
            // finish() for closing.
            // finish();
            // vv.requestFocus();


        }

    });


So that's the logic, I would redesign the selection method or remove it.

/Darren
 
Share this answer
 
Comments
sumanth g 13-Sep-16 6:00am    
I dont want to hard code the path by giving setVideoPath();
what the below code does is " it will display the all the videos from the externel storage on the listview and by click on the list the video is going to play"
From the code i am able to display the list of videos, and don't know how to get the list and play the videos one by one? any help
Darren_vms 13-Sep-16 9:07am    
Its not hard coding, You already have the call vv.setvideopath() in the code you provided, You present a listview the user selects one of the items. You then load the intent extra and call the intent to play the video. However my passing just the filename of the selected item from the listview you sort of shoot yourself in the foot for playing any more. Why don't you pass the media store ?

Your code looks a lot like http://stackoverflow.com/questions/20007342/how-to-get-list-of-video-files-in-a-specific-folder-in-android have you been working on this for two years ?

/Darren
Darren_vms 13-Sep-16 9:10am    
ok so this is the code

http://www.androiddevelopersolutions.com/2013/12/android-play-video-from-sd-card.html



/Darren
sumanth g 13-Sep-16 9:15am    
from this code, we can play the video by clicking on that video, what i want to do is, play all the videos in one by one in the present list.
Darren_vms 13-Sep-16 9:24am    
keep saying the same thing over and over doesn't make it happen, perhaps you should read up on intents and passing between them.

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