我一直在开发Android应用程序,它将浏览内部存储并显示视频,它应该在列表中逐个播放视频。 但我一直在玩视频。它用于扫描列表并用于播放最后一个视频。
package com.com.videolist;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
public class VideoActivity extends Activity {
private Cursor videocursor;
private int video_column_index;
ListView videolist;
int count;
String name = null;
VideoView video_player_view;
DisplayMetrics dm;
SurfaceView sur_View;
MediaController media_Controller;
public class MediaFileInfo {
private String fileName,filePath,fileType;
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
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 };
videocursor = getContentResolver().query(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()));
Log.d("No of Video",""+count);
if(videocursor !=null && videocursor.getCount()>0 && videocursor.moveToFirst()) {
do {
for (int i = 0; i < videocursor.getCount(); i++) {
Log.d("i", String.valueOf(i));
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
// Log.d("getcolumn", String.valueOf(videocursor.moveToPosition(i)));
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
Log.d("name", name);
final int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
// Log.d("column_index", String.valueOf(video_column_index));
// videocursor.moveToPosition(i);
final String filepath = videocursor.getString(column_index);
// Log.d("last i", String.valueOf(i));
Log.d("filepath", filepath);
mediaFileInfo.setFilePath(filepath);
video_player_view = (VideoView) findViewById(R.id.videoView);
media_Controller = new MediaController(this);
dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
video_player_view.setMinimumWidth(width);
video_player_view.setMinimumHeight(height);
video_player_view.setMediaController(media_Controller);
// Log.d("last i", String.valueOf(i));
Log.d("filepath", filepath);
// if(videocursor != null && videocursor.getCount()>0){
// videocursor.moveToFirst();
// do{
// for(i=0;i<videocursor.getCount();i++){
video_player_view.setVideoPath(filepath);
// Log.d("for=last i", String.valueOf(i));
// Log.d("for=filepath", filepath);
video_player_view.requestFocus();
Log.d("videoview","videoview");
video_player_view.start();
}
}
while (videocursor.moveToNext());
}
}
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);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
该应用程序的logcat:
09-15 06:34:40.586 6539-6539/com.com.videolist D/i: 0
09-15 06:34:40.587 6539-6539/com.com.videolist D/name: h.264_acc_f4v.f4v
09-15 06:34:40.587 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.264_acc_f4v.f4v
09-15 06:34:40.612 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.264_acc_f4v.f4v
09-15 06:34:40.614 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.616 6539-6539/com.com.videolist D/i: 1
09-15 06:34:40.616 6539-6539/com.com.videolist D/name: H.264_F4v aac codec.f4v
09-15 06:34:40.617 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_F4v aac codec.f4v
09-15 06:34:40.635 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_F4v aac codec.f4v
09-15 06:34:40.636 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.637 6539-6539/com.com.videolist D/i: 2
09-15 06:34:40.637 6539-6539/com.com.videolist D/name: H.263_mp3_avi.avi
09-15 06:34:40.637 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.263_mp3_avi.avi
09-15 06:34:40.660 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.263_mp3_avi.avi
09-15 06:34:40.662 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.662 6539-6539/com.com.videolist D/i: 3
09-15 06:34:40.663 6539-6539/com.com.videolist D/name: H.264_mp3_flv.flv
09-15 06:34:40.664 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_mp3_flv.flv
09-15 06:34:40.689 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_mp3_flv.flv
09-15 06:34:40.690 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.690 6539-6539/com.com.videolist D/i: 4
09-15 06:34:40.691 6539-6539/com.com.videolist D/name: H.264__aac_flv.flv
09-15 06:34:40.691 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264__aac_flv.flv
09-15 06:34:40.710 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264__aac_flv.flv
09-15 06:34:40.711 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.712 6539-6539/com.com.videolist D/i: 5
09-15 06:34:40.712 6539-6539/com.com.videolist D/name: H.264_aac_m2ts.m2ts
09-15 06:34:40.712 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_m2ts.m2ts
09-15 06:34:40.729 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_m2ts.m2ts
09-15 06:34:40.730 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.730 6539-6539/com.com.videolist D/i: 6
09-15 06:34:40.731 6539-6539/com.com.videolist D/name: H.265_aac_mkv.mkv
09-15 06:34:40.731 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.265_aac_mkv.mkv
09-15 06:34:40.747 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.265_aac_mkv.mkv
09-15 06:34:40.748 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.749 6539-6539/com.com.videolist D/i: 7
09-15 06:34:40.750 6539-6539/com.com.videolist D/name: H.264_amr_nb_3GPP_3g2.3g2
09-15 06:34:40.750 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_amr_nb_3GPP_3g2.3g2
09-15 06:34:40.769 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_amr_nb_3GPP_3g2.3g2
09-15 06:34:40.771 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.771 6539-6539/com.com.videolist D/i: 8
09-15 06:34:40.772 6539-6539/com.com.videolist D/name: h.265_ac3_mp4.mp4
09-15 06:34:40.772 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.265_ac3_mp4.mp4
09-15 06:34:40.817 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.265_ac3_mp4.mp4
09-15 06:34:40.819 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.819 6539-6539/com.com.videolist D/i: 9
09-15 06:34:40.820 6539-6539/com.com.videolist D/name: mpeg2video_aac_ts.ts
09-15 06:34:40.820 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/mpeg2video_aac_ts.ts
09-15 06:34:40.840 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/mpeg2video_aac_ts.ts
09-15 06:34:40.841 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.842 6539-6539/com.com.videolist D/i: 10
09-15 06:34:40.842 6539-6539/com.com.videolist D/name: flv_mp3_flv.flv
09-15 06:34:40.842 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/flv_mp3_flv.flv
09-15 06:34:40.863 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/flv_mp3_flv.flv
09-15 06:34:40.864 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.864 6539-6539/com.com.videolist D/i: 11
09-15 06:34:40.865 6539-6539/com.com.videolist D/name: mpeg2video_mp2_mpg.mpg
09-15 06:34:40.865 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/mpeg2video_mp2_mpg.mpg
09-15 06:34:40.870 6539-6612/com.com.videolist W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException: Attempt to read from field 'android.view.HardwareRenderer android.view.View$AttachInfo.mHardwareRenderer' on a null object reference
at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:504)
at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1104)
at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:560)
at android.os.Binder.execTransact(Binder.java:453)
09-15 06:34:40.907 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/mpeg2video_mp2_mpg.mpg
09-15 06:34:40.908 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.908 6539-6539/com.com.videolist D/i: 12
09-15 06:34:40.909 6539-6539/com.com.videolist D/name: rawvideo_pcm_s16le_avi.avi
09-15 06:34:40.910 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/rawvideo_pcm_s16le_avi.avi
09-15 06:34:40.932 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/rawvideo_pcm_s16le_avi.avi
09-15 06:34:40.934 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.934 6539-6539/com.com.videolist D/i: 13
09-15 06:34:40.934 6539-6539/com.com.videolist D/name: xvid_aac_mpeg4_mp4.mp4
09-15 06:34:40.935 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/xvid_aac_mpeg4_mp4.mp4
09-15 06:34:40.958 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/xvid_aac_mpeg4_mp4.mp4
09-15 06:34:40.959 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.960 6539-6539/com.com.videolist D/i: 14
09-15 06:34:40.960 6539-6539/com.com.videolist D/name: MPEG4_mp4.mp4
09-15 06:34:40.961 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/MPEG4_mp4.mp4
09-15 06:34:40.975 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/MPEG4_mp4.mp4
09-15 06:34:40.976 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.976 6539-6539/com.com.videolist D/i: 15
09-15 06:34:40.977 6539-6539/com.com.videolist D/name: H.265_aac_mp4.mp4
09-15 06:34:40.977 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.265_aac_mp4.mp4
09-15 06:34:40.992 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.265_aac_mp4.mp4
09-15 06:34:40.993 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:40.994 6539-6539/com.com.videolist D/i: 16
09-15 06:34:40.994 6539-6539/com.com.videolist D/name: h.263_amr_nb_3gp.3gp
09-15 06:34:40.994 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.263_amr_nb_3gp.3gp
09-15 06:34:41.006 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h.263_amr_nb_3gp.3gp
09-15 06:34:41.007 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:41.007 6539-6539/com.com.videolist D/i: 17
09-15 06:34:41.008 6539-6539/com.com.videolist D/name: H.264_mp3_mkv.mkv
09-15 06:34:41.008 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_mp3_mkv.mkv
09-15 06:34:41.020 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_mp3_mkv.mkv
09-15 06:34:41.021 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:41.022 6539-6539/com.com.videolist D/i: 18
09-15 06:34:41.022 6539-6539/com.com.videolist D/name: H.264_aac_mp4.mp4
09-15 06:34:41.022 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_mp4.mp4
09-15 06:34:41.035 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_mp4.mp4
09-15 06:34:41.038 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:41.038 6539-6539/com.com.videolist D/i: 19
09-15 06:34:41.039 6539-6539/com.com.videolist D/name: H.264_aac_mkv.mkv
09-15 06:34:41.039 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_mkv.mkv
09-15 06:34:41.053 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/H.264_aac_mkv.mkv
09-15 06:34:41.054 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:41.054 6539-6539/com.com.videolist D/i: 20
09-15 06:34:41.055 6539-6539/com.com.videolist D/name: h264_acc_flv.flv
09-15 06:34:41.055 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h264_acc_flv.flv
09-15 06:34:41.066 6539-6539/com.com.videolist D/filepath: /storage/emulated/0/Movies/Videos/h264_acc_flv.flv
09-15 06:34:41.067 6539-6539/com.com.videolist D/videoview: videoview
09-15 06:34:41.275 6539-6557/com.com.videolist W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException: Attempt to read from field 'android.view.HardwareRenderer android.view.View$AttachInfo.mHardwareRenderer' on a null object reference
at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:504)
at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1104)
at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:560)
at android.os.Binder.execTransact(Binder.java:453)
09-15 06:34:41.275 6539-6539/com.com.videolist I/Choreographer: Skipped 51 frames! The application may be doing too much work on its main thread.
09-15 06:34:41.516 6539-6539/com.com.videolist W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Movies/Videos/h264_acc_flv.flv
09-15 06:34:41.763 6539-6539/com.com.videolist D/MediaPlayer: getMetadata
09-15 06:34:41.823 6539-6556/com.com.videolist W/MediaPlayer: info/warning (3, 0)
09-15 06:34:43.241 6539-6552/com.com.videolist W/CursorWrapperInner: Cursor finalized without prior close()