无法在列表视图的videoview中播放视频

时间:2018-08-28 19:55:35

标签: android listview android-videoview

我无法通过相同布局的列表视图在videoview上播放视频。 单击列表视图项时,它会显示一条消息“无法播放此视频”。 我已经在清单中选了READ_EXTERNAL_PERMISSION。 我手机中的所有视频都显示在列表视图中,但该视频无法播放。

 // create a new object array
 var sort : [MyCustomObject] = []

//iterate through my object array  

        for i in objectArray{
            if i === objectToRemove{
            }
            else{
                sort.append(i)
            }
        }

     // set old array to sort, which no longer has the unwanted object 
        self.objectArray = sort

}

1 个答案:

答案 0 :(得分:0)

这是我的代码

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_a1);
            listView1 = findViewById(R.id.lv1);
            videoView1 = findViewById(R.id.vv1);
            mediaController = new MediaController(A1.this);

            if (ContextCompat.checkSelfPermission(A1.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(A1.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    ActivityCompat.requestPermissions(A1.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
                } else {
                    ActivityCompat.requestPermissions(A1.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
                }
            } else {
                doStuff();
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSION_REQUEST: {
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        if (ContextCompat.checkSelfPermission(A1.this,
                                Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                            Toast.makeText(A1.this, "Permission Granted", Toast.LENGTH_SHORT).show();
                            doStuff();
                        } else {
                            Toast.makeText(A1.this, "No Permission granted", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                }
            }
        }

        public void doStuff() {
            arrayList = new ArrayList<>();
            ContentResolver contentResolver = getContentResolver();
            Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            final Cursor videoCursor = contentResolver.query(videoUri, null, null, null, null);

            if (videoCursor != null && videoCursor.moveToFirst()) {
                int songTitle = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);

                do {
                    String currentTitle = videoCursor.getString(songTitle);
                    arrayList.add(currentTitle);
                } while (videoCursor.moveToNext());
            }
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
            listView1.setAdapter(adapter);

            listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, 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);
                    videoView1.setVideoPath(filename);
                    videoView1.setMediaController(new MediaController(A1.this));
                    videoView1.requestFocus();
                    videoView1.start();
                }
            });
        }
    }