无法从列表视图播放 videoview 中的视频
Can't play video in videoview from listview
我无法通过相同布局的列表视图在 videoview 上播放视频。
单击列表视图项时,它会显示一条消息 "Can't play this video"。
我在 Manifest 中取了 READ_EXTERNAL_PERMISSION。
我 phone 中的所有视频都显示在列表视图中,但视频无法播放。
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();
}
}
public void doStuff(){
arrayList = new ArrayList<>();
getVideo();
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) {
String myUrl = adapter.getItem(position);
Uri uri = Uri.parse(myUrl);
videoView1.setVideoURI(uri);
videoView1.start();
}
});
}
public void getVideo(){
ContentResolver contentResolver = getContentResolver();
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Cursor videoCursor = contentResolver.query(videoUri,null,null,null,null);
if(videoCursor != null && videoCursor.moveToFirst()) {
int songTitle = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);
int songAlbum = videoCursor.getColumnIndex(MediaStore.Video.Media.ALBUM);
do{
String currentTitle = videoCursor.getString(songTitle);
String currentAlbum = videoCursor.getString(songAlbum);
arrayList.add(currentTitle + "\n" + currentAlbum);
}while(videoCursor.moveToNext());
}
}
@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();
}
return;
}
}
}
}
}
这是我的代码
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();
}
});
}
}
我无法通过相同布局的列表视图在 videoview 上播放视频。 单击列表视图项时,它会显示一条消息 "Can't play this video"。 我在 Manifest 中取了 READ_EXTERNAL_PERMISSION。 我 phone 中的所有视频都显示在列表视图中,但视频无法播放。
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();
}
}
public void doStuff(){
arrayList = new ArrayList<>();
getVideo();
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) {
String myUrl = adapter.getItem(position);
Uri uri = Uri.parse(myUrl);
videoView1.setVideoURI(uri);
videoView1.start();
}
});
}
public void getVideo(){
ContentResolver contentResolver = getContentResolver();
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Cursor videoCursor = contentResolver.query(videoUri,null,null,null,null);
if(videoCursor != null && videoCursor.moveToFirst()) {
int songTitle = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);
int songAlbum = videoCursor.getColumnIndex(MediaStore.Video.Media.ALBUM);
do{
String currentTitle = videoCursor.getString(songTitle);
String currentAlbum = videoCursor.getString(songAlbum);
arrayList.add(currentTitle + "\n" + currentAlbum);
}while(videoCursor.moveToNext());
}
}
@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();
}
return;
}
}
}
}
}
这是我的代码
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();
}
});
}
}