<ListView android:layout_width="match_parent" android:id="@+id/lv" android:layout_height="match_parent"></ListView> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <SurfaceView android:id="@+id/surface_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </FrameLayout> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
public class MyAdapter extends BaseAdapter { private List<VideoItem> list; private Context context; private LayoutInflater inflater; private MediaPlayer player; //当前播放item的position,-1表示没有任何item在播放 private int currentPlayPosition = -1; private int prePosition = -1; public MyAdapter(Context context, List<VideoItem> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); player = new MediaPlayer(); player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { //异步准备完成时调用start方法开始播放 @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ListView listView = (ListView) parent; int firstVisiblePosition = listView.getFirstVisiblePosition(); if (prePosition != firstVisiblePosition) { if (prePosition == currentPlayPosition) { player.stop(); currentPlayPosition = -1; } prePosition = firstVisiblePosition; } Log.d("google_lenve_fb", "getView: position:" + position); ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.listview_item, null); holder = new ViewHolder(); holder.content = (TextView) convertView.findViewById(R.id.content); holder.cover = (ImageView) convertView.findViewById(R.id.iv); holder.surfaceView = (SurfaceView) convertView.findViewById(R.id.surface_view); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } VideoItem videoItem = list.get(position); holder.content.setText(videoItem.getContent()); //加载缩略图 Picasso.with(context).load(videoItem.getCover()).placeholder(R.mipmap.ic_launcher).into(holder.cover); //重新设置图片和SurfaceView的宽和高 ViewGroup.LayoutParams lp = holder.surfaceView.getLayoutParams(); lp.width = videoItem.getWidth(); lp.height = videoItem.getHeight(); holder.surfaceView.setLayoutParams(lp); holder.cover.setLayoutParams(lp); /*************处理播放****************/ // //获取每张图片所存在 // Object tag = holder.cover.getTag(); // //当tag不为空时,说明该item是被回收的item,而不是第一屏的item // if (tag != null) { // Integer tag1 = (Integer) tag; // //当item被移出屏幕时,停止播放 // if (tag1 == currentPlayPosition && tag1 != position) { // player.stop(); // currentPlayPosition = -1; // } // } //将当前item的position保存为图片的tag holder.cover.setTag(position); String p360 = videoItem.getP360(); //判断当前播放的item是不是即将要显示的item if (currentPlayPosition == position) { holder.cover.setVisibility(View.INVISIBLE); holder.surfaceView.setVisibility(View.VISIBLE); //重置MediaPlayer player.reset(); //设置显示视频需要的SurfaceHolder player.setDisplay(holder.surfaceView.getHolder()); try { //设置数据源 player.setDataSource(context, Uri.parse(p360)); //异步准备 player.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } } else { holder.surfaceView.setVisibility(View.INVISIBLE); holder.cover.setVisibility(View.VISIBLE); } holder.cover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.setVisibility(View.INVISIBLE); //获得当前ImageView中携带的tag,由该tag可以获得要播放第几个item Integer tag = (Integer) v.getTag(); //当点击ImageView时,如果已经有item在播放,则先停止播放 if (player != null && player.isPlaying()) { player.stop(); } currentPlayPosition = tag; //刷新列表,重新刷新列表会调用getView方法 notifyDataSetChanged(); } }); return convertView; } public void stop() { //停止播放并释放资源 if (player != null) { player.stop(); player.release(); } } class ViewHolder { SurfaceView surfaceView; ImageView cover; TextView content; } }
public class MainActivity extends AppCompatActivity { private List<VideoItem> list; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.lv); initData(); adapter = new MyAdapter(this, list); listView.setAdapter(adapter); } private void initData() { String jsonStr = getJsonString(); parseJson(jsonStr); } private void parseJson(String jsonStr) { list = new ArrayList<>(); try { JSONObject jo = new JSONObject(jsonStr); JSONObject data = jo.getJSONObject("data"); JSONArray dataArr = data.getJSONArray("data"); for (int i = 0; i < dataArr.length(); i++) { JSONObject item = dataArr.getJSONObject(i); String p360_video = null; int video_height = 0; int video_width = 0; String cover = null; String content = null; try { JSONObject group = item.getJSONObject("group"); p360_video = group.getJSONObject("360p_video").getJSONArray("url_list").getJSONObject(0).getString("url"); video_height = group.getInt("video_height"); video_width = group.getInt("video_width"); cover = group.getJSONObject("medium_cover").getJSONArray("url_list").getJSONObject(0).getString("url"); content = group.getString("content"); VideoItem videoItem = new VideoItem(content, cover, video_height, p360_video, video_width); list.add(videoItem); } catch (JSONException e) { // e.printStackTrace(); //在这里处理异常数据... } } } catch (JSONException e) { e.printStackTrace(); } } private String getJsonString() { InputStream is = null; StringBuffer result = new StringBuffer(); try { is = getAssets().open("video1.json"); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { result.append(new String(buf, 0, len)); } return result.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override protected void onStop() { super.onStop(); adapter.stop(); } }