这是我的代码。
public class MainActivity extends Activity {
ListView list;
public ArrayList<String> videoList;
public CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView)findViewById(R.id.list);
videoList = new ArrayList<String>();
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("http://www.youtube.com/watch?v=DdlWPL53PvQ#sthash.fW5EtDFb.dpuf");
Log.d("size of videoList:",""+ videoList.size());
adapter = new CustomListAdapter(MainActivity.this, videoList);
list.setAdapter(adapter);
}
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private ArrayList<String> videoList;
public CustomListAdapter(Activity activity, ArrayList<String> videoList) {
this.activity = activity;
this.videoList = videoList;
}
@Override
public int getCount() {
return videoList.size();
}
@Override
public Object getItem(int location) {
return videoList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
VideoView video = (VideoView)convertView.findViewById(R.id.video_view);
video.setVideoPath(videoList.get(position));
video.start();
return convertView;
}
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />
</RelativeLayout>
list_raw.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp" >
<!-- Thumbnail Image -->
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</RelativeLayout>
我无法获取视频,并且logcat中没有任何错误显示然后是什么问题? 我在清单中添加了互联网许可,是否需要任何许可或其他任何设置?
答案 0 :(得分:1)
我认为我们不应该直接在列表中放置视频视图,因为列表中的每个视图都被重绘或无效很多次,如果用户正在播放和观看任何视频,并且他不小心将其滚出屏幕, Android操作系统将释放该内存,因此将再次从Start播放视频。 会一次又一次地调用Video.start()
最佳解决方案是提供任何视频帧的缩略图。 用图片视图替换视频视图,然后点击它打开一个新视频,放置视频视图并播放。
答案 1 :(得分:0)
尝试设置VideoURI
代替VideoPath
,然后再次检查,
VideoView video = (VideoView)convertView.findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setVideoURI(Uri.parse(videoList.get(position)));
video.start();
更新:如果您计划仅使用YouTube上的视频,那么最好的方法是使用官方YouTube API。点击这里,