Android browse the local specified path video file and play it

The project needs to open a local folder under a specified path, get the video, and then call a player to play it. To get local videos and generate thumbnails, I found two ideas:

1. Use the Android system's own browser

This method does not need to define a layout file for image display, just write a few lines of code. The disadvantage is that all mp4 and 3gp videos in the storage will be obtained, and the opening path cannot be specified, so it is not used because it does not meet the project requirements.

First define a button to open the browser:

Button button2 = (Button) findViewById(R.id.xuanshipin);
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("video/*");//这里是设置打开文件的类型,也可以换成图片

                startActivityForResult(intent, VIDEO_CAPTURE1);//定义处理浏览结果的类,也就是说点击某一个文件后需要执行的操作,VIDEO_CAPTURE是参数,在多个按钮时使用
            }
        });

The class definition of the processing result is as follows:
an imageview is defined here to display the first frame of the selected video, so remember to declare at the beginning, bitmap and imageview

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK && requestCode == VIDEO_CAPTURE0) {
            Uri uri = data.getData();
            Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex(VideoColumns._ID));
                String filePath = cursor.getString(cursor.getColumnIndex(VideoColumns.DATA));
                 bitmap = Thumbnails.getThumbnail(getContentResolver(), id, Thumbnails.MICRO_KIND, null);
                // ThumbnailUtils类2.2以上可用
                // Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath,
                // Thumbnails.MICRO_KIND);
                imageView.setImageBitmap(bitmap);
                Log.d("ddd", "filepath==" + filePath);
                File file=new File(filePath);
                cursor.close();

            }
        }

        else if (resultCode == Activity.RESULT_OK && requestCode == VIDEO_CAPTURE1) {
            Uri uri = data.getData();
            File file = getFileByUri(uri);
             MediaMetadataRetriever mmr=new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象  
             mmr.setDataSource(file.getAbsolutePath());
             bitmap=mmr.getFrameAtTime(2000);//获得视频第一帧的Bitmap对象
             String duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);//时长(毫秒)
             Log.d("ddd","duration=="+duration);
            int int_duration= Integer.parseInt(duration);

             if(int_duration>45000){
                 Toast.makeText(getApplicationContext(), "视频时长超过45秒请重新选择", Toast.LENGTH_SHORT).show();;

             }
             imageView.setImageBitmap(bitmap);

        }

This method opens the browser quickly and the user experience is good.

2. Define your own listview

The disadvantage is that the amount of code is large, and the time-consuming operation will not be processed

First look at the layout file, in fact, it is not very troublesome:
the entire listview under the linear layout

<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"
    tools:context=".Choose_video" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:id="@+id/lv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    </LinearLayout>

</RelativeLayout>

Then there is the operation of filling the layout with the preview image of the file, which is directly an activity.

public class Choose_video extends Activity implements OnItemClickListener{

    private String cur_path=Environment.getExternalStorageDirectory().toString()+"/VRDemo/video/";//这里就是自定义打开路径啦
    private List<Picture> listPictures;
    ListView listView ;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                List<Picture> listPictures = (List<Picture>) msg.obj;
                MyAdapter adapter = new MyAdapter(listPictures);
                listView.setAdapter(adapter);
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_video);
        new Thread(new Runnable() {

            @Override
            public void run() {

                loadVaule();//oncreate()里定义这个方法特别耗时,导致整个界面启动超级慢,需要4/5秒的样子

            }
        }).start();
    }
    //下面就看看这个方法里有多耗时
    private void loadVaule(){
        File file = new File(cur_path);
        File[] files  = null;
        files = file.listFiles();
        listPictures = new ArrayList<Picture>();
        for (int i = 0; i < files.length; i++) {
            Picture picture = new Picture();
            picture.setBitmap(getVideoThumbnail(files[i].getPath(), 200, 200, MediaStore.Images.Thumbnails.MICRO_KIND));
            picture.setPath(files[i].getPath());
            listPictures.add(picture);

        }
        listView = (ListView) findViewById(R.id.lv_show);
        listView.setOnItemClickListener(this);
        Message msg = new Message();
        msg.what = 0;
        msg.obj = listPictures;

        handler.sendMessage(msg);

    }


      //获取视频的缩略图
    private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {   
           Bitmap bitmap = null;  
                // 获取视频的缩略图  
           bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);  
//         System.out.println("w"+bitmap.getWidth());  
//         System.out.println("h"+bitmap.getHeight());  
           bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,  
           ThumbnailUtils.OPTIONS_RECYCLE_INPUT);  
           return bitmap;  
    }


//定义适配器放listview                
public class MyAdapter extends BaseAdapter {
                    private List<Picture> listPictures;

                    public MyAdapter(List<Picture> listPictures) {
                        super();
                        this.listPictures = listPictures;

                    }

                    @Override
                    public int getCount() {
                        // TODO Auto-generated method stub
                        return listPictures.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        // TODO Auto-generated method stub
                        return listPictures.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        // TODO Auto-generated method stub
                        return position;
                    }

                    @Override
                    public View getView(int position, View v, ViewGroup arg2) {
                        // TODO Auto-generated method stu
                        View view = getLayoutInflater().inflate(R.layout.item,null);
                        ImageView imageView = (ImageView) view.findViewById(R.id.iv_show);
                        TextView textView = (TextView) view.findViewById(R.id.tv_show);

                        imageView.setImageBitmap(listPictures.get(position).getBitmap());
                        textView.setText(listPictures.get(position).getPath());
                        return view;

                    }
                }





    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
        // TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "点击了"+arg2, 200).show();
    startPlay(this, listPictures.get(arg2).getPath());//点击后打开播放器
    Log.e("path", listPictures.get(arg2).getPath());
                }


public static void startPlay(Context context, String url) {
                    if (url.contains(".mp4") || url.contains(".MP4")) {
                        Intent intent = new Intent();
                        intent.setClassName(context, "com.easemob.chatuidemo.activity.VideoPlayerActivity");
                        intent.putExtra("playpath", url);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }else if(url.contains("rtsp") || url.contains("RTSP")){
                        Intent intent = new Intent();
                        intent.setClassName(context, "com.easemob.chatuidemo.activity.MakeVideoActivity");
                        intent.putExtra("playpath", url);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    } else if (url.contains(".jpg") || url.contains(".png")) {
                        Intent intent = new Intent();
                        intent.setClassName(context, "com.easemob.chatuidemo.activity.VideoPlayerActivity");
                        intent.putExtra("playpath", "/sdcard/VRDemo/capture/aa.jpg");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

                    }else {
                        Toast.makeText(context, "没有资源哦", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setClassName(context, "com.easemob.chatuidemo.activity.MainActivity");
                    }
                }


}

There are solutions for time-consuming operations in onCreate():
(1) Do as little as possible before the Activity starts.
(2) When the layout is more complex, you can consider not to load all at one time, dynamic loading is a good way.
(3) For the data that is needed in time, it is time-consuming and extremely dangerous to load, you must remember to open a thread to do these actions, and do not do anything that blocks the main thread (UI thread).
(4) For special cases, when Activity startup does require a lot of work, you can consider loading a simple layout (or Activity) to transition.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325349339&siteId=291194637