我正在制作视频的ListView,所以我决定使用视频观看。我知道鼓励使用TextureViews来显示视频而不是VideoView,但我认为VideoView应该可以正常使用。问题是我的视频没有显示出来。我知道我的视频路径很好,因为如果我将文件路径硬编码到listview的外部,那么它就会很好地显示出来。此外,如果我将视频路径添加到textview而不是listview中的视频视图,那么它会很好地显示它们。问题仅在于视频观看。任何帮助将不胜感激。
public class VideoListFragment extends Fragment {
private Activity currentActivity;
private long projectId;
VideoAdapter videoAdapter;
List<String> videoPathList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.video_list, container, false);
currentActivity = getActivity();
projectId = ((Project)currentActivity).projectId;
List<String> newList = new ArrayList<String>();
ListView videoList = (ListView) rootView.findViewById(R.id.videoList);
File directory = new File(getContext().getExternalFilesDir(null) + File.separator + projectId + File.separator);
File[] dirContentsList = directory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.endsWith(".mp4");
}
});
Pair[] pairs = new Pair[dirContentsList.length];
for (int i = 0; i < dirContentsList.length; i++)
pairs[i] = new Pair(dirContentsList[i]);
// Sort them by timestamp.
Arrays.sort(pairs);
// Take the sorted pairs and extract only the file part, discarding the timestamp.
for (int i = 0; i < dirContentsList.length; i++)
newList.add(pairs[i].filePath);
videoPathList = new ArrayList(Arrays.asList(Arrays.toString(dirContentsList).split(",")));
videoAdapter = new VideoAdapter(getContext(), R.layout.video_list, newList);
videoList.setAdapter(videoAdapter);
videoAdapter.addAll(newList);
videoAdapter.notifyDataSetChanged();
return rootView;
}
private class VideoAdapter extends ArrayAdapter<String> {
public VideoAdapter(Context context, int resource, List<String> videos) {
super(context, resource, videos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(getContext());
convertView = mInflater.inflate(R.layout.video_row, null);
holder.videoView = (VideoView) convertView.findViewById(R.id.videoView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String videoPath = getItem(position);
holder.videoView.setVideoPath(videoPath);
return convertView;
}
}
public static class ViewHolder {
public VideoView videoView;
}
class Pair implements Comparable {
public long t;
public File f;
public String filePath;
public Pair(File file) {
f = file;
t = file.lastModified();
filePath = file.getPath();
}
public int compareTo(Object o) {
long u = ((Pair) o).t;
return t < u ? -1 : t == u ? 0 : 1;
}
};
}
video_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoList"
/>
</RelativeLayout>
video_row.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:layout_marginLeft="65dp"
android:layout_marginStart="65dp"
android:layout_marginTop="25dp"/>
查看树
android.widget.RelativeLayout{d0fdaf6 V.E..... ... -1440,0-0,2463}
android.widget.ListView{c76c3fb VFED.VC. F.. 0,0-1440,28 #7f0c009b app:id/videoList}
android.widget.VideoView{d6a1c18 VFE..... ... 0,0-1440,0 #7f0c0099 app:id/videoView}
android.widget.VideoView{d4d7671 VFE..... ... 0,4-1440,4 #7f0c0099 app:id/videoView}
android.widget.VideoView{780c56 VFE..... ... 0,8-1440,8 #7f0c0099 app:id/videoView}
android.widget.VideoView{763a4d7 VFE..... ... 0,12-1440,12 #7f0c0099 app:id/videoView}
android.widget.VideoView{6f0e1c4 VFE..... ... 0,16-1440,16 #7f0c0099 app:id/videoView}
android.widget.VideoView{af208ad VFE..... ... 0,20-1440,20 #7f0c0099 app:id/videoView}
android.widget.VideoView{a8097e2 VFE..... ... 0,24-1440,24 #7f0c0099 app:id/videoView}