Home  >  Q&A  >  body text

java - 学习做一个安卓视频播放器,有一些小问题!忘大家请教

最近在学习安卓开发,有一些JAVA基础,想做一个简单的视频播放器,现在卡到了获取视频文件路径这一点上,现在已经会用Mediaplay,video等控件,但是只能每一次在AS中输入程序路径,想能够在程序上去获取本地所有视频路径,然后可以选择播放,可是网上查了很多,很多代码也运行不起也看的不是很懂,所以希望大家能给一个思路,我好在去学习。这个查了快一点了,也没有解决!谢谢大家

迷茫迷茫2744 days ago693

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:51:29

    Two ways

    1. It’s simple. Call the file selector in the system to help you find the file you need and return the path to you. The code is small and easy. It’s as simple as this:

    private void pickFile() {
        Intent intent = new Intent();
        //意图类型过滤,指定视频类型文件
        intent.setType("video/*");
        //意图动作,选取内容
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 0x01);
    }
    
    .
    .
    .
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            //获取文件路径
            String path = cursor.getString(1);
        }
    }
    

    2. Implement your own file manager and filter the file types you need by scanning the files on the system memory. It is a bit more complicated. It involves ContentResolver,Uri,Cursor,MediaStore,MimeType,Intent and other major knowledge points. It is definitely complicated and troublesome. Don't be afraid of trouble when you make something good, just go and eat it.

    The first way lets you know what it is, and the second way lets you know why.

    Write the word "wang" correctly, not "forget".

    The second method is to give you these references and follow them to implement it yourself. That’s almost it.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:51:29

    First of all, let me solve your doubts about how to obtain all local video paths. When the Android system stores video, audio, pictures and other resources, it will automatically store its related information in the database. The information includes name, size, storage path, etc. If we play a certain video file, get its storage path from the database, and then get the video itself through the path. Then you need to do the following things:
    1. Understand the name and fields of the database that stores Android video information
    2. Understand the method of obtaining Android to obtain data in the database. Android has been packaged
    Look at the code for scanning all Video information. I searched casually, so I'm not sure it's correct. I'll explain it to you.

    private void scanVideoUri(){
            //通过ContentResolver从数据库获取信息,Curse是获取的结果
            Cursor cursor = mContext.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                            null, null, null, null);
            int totalCount =cursor.getCount();//计算所有结果的条数
            cursor.moveToFirst();//此句一定要有
            //遍历所有的Video信息
            for( int i = 0;i < totalCount;i++){
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
                String data1 = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
                String type = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));
                Log.e(TAG, data+title+type);
                cursor.moveToNext();//访问下一个
            }
        }
        

    It’s normal if you don’t understand the code. Break the target down and then splice it together to get the final result.

    reply
    0
  • 阿神

    阿神2017-04-18 10:51:29

    Read the system database, and all the videos that can be seen in your mobile phone video list can be directly searched. For specific implementation, you can ask Du Niang~

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:51:29

    http://blog.csdn.net/github_3... This tutorial is good

    reply
    0
  • Cancelreply