ListVideoActivity
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.animee.day20.R;
import com.animee.day20.UniteApp;
import java.util.ArrayList;
import java.util.List;
public class ListVideoActivity extends AppCompatActivity {
private ListView mListView;
private VideoListAdapter adapter;
private List
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_video);
mListView = (ListView) findViewById(R.id.id_lv);
mDatas = new ArrayList<>();
adapter = new VideoListAdapter(this,mDatas);
mListView.setAdapter(adapter);
initDialog();
setData(NetConfig.VIDEO_PATH);
}
private void initDialog(){
dialog = new ProgressDialog(this);
dialog.setMessage("正在加载中.....");
dialog.setTitle("提示信息");
}
private void setData(String url){
dialog.show();
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener
@Override
public void onResponse(String s) {
dialog.dismiss();
if (!TextUtils.isEmpty(s)) {
List
if (list!=null&&list.size()!=0) {
mDatas.addAll(list);
adapter.notifyDataSetChanged();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
dialog.dismiss();
Toast.makeText(ListVideoActivity.this,"网络加载失败,请检查网络!!",Toast.LENGTH_LONG).show();
}
});
UniteApp.getHttpQueue().add(request);
}
}
NetConfig
public interface NetConfig {
String VIDEO_PATH = "http://m2.qiushibaike.com/article/list/video?page=2&count=30&readarticles=[115762484,115762135,115764350,115761463,115760316,115764445,115763537,115758684]&rqcnt=17&r=804df97a1459411164081";
}
ParseVideoBean
import com.google.gson.Gson;
import java.util.List;
/**
* Created by Administrator on 2017/1/18.
*/
public class ParseVideoBean {
private List
public List
return items;
}
public void setItems(List
this.items = items;
}
public static List
return new Gson().fromJson(json,ParseVideoBean.class).getItems();
}
public class VideoBean{
private String high_url;
private String pic_url;
private String content;
private String low_url;
public VideoBean(String high_url, String pic_url, String content, String low_url) {
this.high_url = high_url;
this.pic_url = pic_url;
this.content = content;
this.low_url = low_url;
}
public VideoBean() {
}
public String getHigh_url() {
return high_url;
}
public void setHigh_url(String high_url) {
this.high_url = high_url;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getLow_url() {
return low_url;
}
public void setLow_url(String low_url) {
this.low_url = low_url;
}
}
}
VideoListAdapter
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.animee.day20.R;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.util.List;
/**
* Created by Administrator on 2017/1/18.
*/
public class VideoListAdapter extends BaseAdapter implements View.OnClickListener{
private Context context;
private LayoutInflater inflater;
private List
private MediaPlayer mediaPlayer;
//明确在整个列表中,同一时间只有一个会被播放,所以一个mediaplayer就够了
private int mCurrentPosition = -1;
public VideoListAdapter(Context context, List
this.context = context;
this.mDatas = mDatas;
inflater = LayoutInflater.from(context);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mCurrentPosition = -1;
notifyDataSetChanged(); //当前视频播放结束了,原来需要播放的不播放了,就通知adapter更新
}
});
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView==null) {
convertView = inflater.inflate(R.layout.item_video,parent,false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ParseVideoBean.VideoBean videoBean = mDatas.get(position);
viewHolder.mContentTv.setText(videoBean.getContent());
if (!TextUtils.isEmpty(videoBean.getPic_url())) {
Picasso.with(context).load(videoBean.getPic_url()).into(viewHolder.mDisplayIv);
}
//明确surfaceview播放的条件,播放的位置和当前绘制的位置相等,当前绘制的位置就显示surfaceview
//并且播放mediaplayer,然后隐藏图片,如果不相等,就隐藏surfaceview不播放
//播放的位置怎么改变,怎么获取
//点击图片,然后被点击图片的位置就是要播放视频的位置
viewHolder.mDisplayIv.setOnClickListener(this);
//怎么传递当前的位置呢?可以给每个图片设置tag
viewHolder.mDisplayIv.setTag(position);
//判断当前位置是显示图片缩略图还是显示视频,就需要看视频播放的位置和当前的位置是否相等。
if (mCurrentPosition==position) { //这个位置需要播放视频
String low_url= videoBean.getLow_url();
viewHolder.mSurfaceView.setVisibility(View.VISIBLE);
viewHolder.mDisplayIv.setVisibility(View.INVISIBLE);
//判断当前位置是否播放,如果播放就暂停
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
//因为视频播放的地址发生变化了,需要重置播放器
mediaPlayer.reset();
//获取当前surfaceview的surfaceholder对象
SurfaceHolder surfaceHolder = viewHolder.mSurfaceView.getHolder();
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(context, Uri.parse(low_url));
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}else { //不播放视频,显示图片
viewHolder.mDisplayIv.setVisibility(View.VISIBLE);
viewHolder.mSurfaceView.setVisibility(View.INVISIBLE);
}
return convertView;
}
@Override
public void onClick(View v) {
//哪个图片被点击了,哪个图片的点击事件就会被触发,可以拿到点击的位置
int pos= (Integer) v.getTag();
if (pos!=-1) {
mCurrentPosition = pos;
notifyDataSetChanged();
//因为点击之后,点击位置的视频播放了,有的视频停止了,列表视图改变了,所以要提示更新
}
}
class ViewHolder{
SurfaceView mSurfaceView;
ImageView mDisplayIv;
TextView mContentTv;
public ViewHolder(View itemView){
mSurfaceView = (SurfaceView)itemView.findViewById(R.id.id_item_surface);
mDisplayIv = (ImageView)itemView.findViewById(R.id.id_item_thumb);
mContentTv = (TextView)itemView.findViewById(R.id.id_item_tv);
}
}
}
activity_list_video
android:id="@+id/activity_list_video"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
item_video
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="220dp" />
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示内容"
android:textSize="26sp"
android:textColor="@color/colorAccent"
android:layout_margin="10dp"/>