这是我的代码。
public class MainActivity extends Activity {
ListView listview;
CustomListAdapter adapter;
private static ProgressDialog progressDialog;
public static int currentItem;
public static ArrayList<String> videoLIst;
public static MediaController mediaController ;
boolean pauseOnScroll = true;
boolean pauseOnFling = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
videoLIst = new ArrayList<String>();
mediaController = new MediaController(MainActivity.this);
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
listview = (ListView) findViewById(R.id.list);
//adapter = new CustomAdapter(MainActivity.this, videoLIst);
// Set video link (mp4 format )
listview.setAdapter(new CustomListAdapter(this, videoLIst));
listview.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
// TODO Auto-generated method stub
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
if(videoView != null)
{
videoView.stopPlayback();
}
else
{
videoView.start();
}
}
});
}
static class ViewHolderItem {
TextView textViewItem;
VideoView video;
ProgressBar progressbar;
}
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;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolderItem viewHolder;
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = inflater.inflate(R.layout.activity_main, null);
viewHolder = new ViewHolderItem();
viewHolder.video = (VideoView)convertView.findViewById(R.id.VideoView);
viewHolder.progressbar = (ProgressBar)convertView.findViewById(R.id.progress);
viewHolder.textViewItem =(TextView)convertView.findViewById(R.id.txvposition);
viewHolder.textViewItem.setText("Video"+position);
mediaController.setAnchorView(viewHolder.video);
Log.d("video url inadapter:", ""+videoLIst.toString());
Uri uri = Uri.parse(videoList.get(position));
viewHolder.video.setMediaController(mediaController);
viewHolder.video.setVideoURI(uri);
//viewHolder.video.start();
viewHolder.video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
viewHolder.progressbar.setVisibility(View.GONE);
viewHolder.video.start();
}
});
viewHolder.video.setOnInfoListener(new OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.d("BUFFRRING START", "On_InFO");
if (MediaPlayer.MEDIA_INFO_BUFFERING_START == what) {
viewHolder.progressbar.setVisibility(View.VISIBLE);
Log.d("BUFFRRING START", "" + what);
}
if (MediaPlayer.MEDIA_INFO_BUFFERING_END == what) {
viewHolder.progressbar.setVisibility(View.GONE);
Log.d("BUFFRRING END", "" + what);
}
if (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START == what) {
viewHolder.progressbar.setVisibility(View.GONE);
Log.d("RENDERING END", "" + what);
}
return false;
}
});
viewHolder.video.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.d("Error", "------>error" + what + "/" + extra);
if (extra != 0) {
Toast.makeText(MainActivity.this, "Can't Play Video",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
int pos;
pos=viewHolder.video.getCurrentPosition();
Log.d("current item position:",""+pos);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
return convertView;
}
}
}
我希望功能像藤蔓应用程序,当视频在列表视图中可见时它会播放,当我向上滚动列表视图时它会变得不可见它会暂停,当我向下滚动时它再次从它停止的地方恢复播放;我的问题是当我滚动时列表视图视频暂停但当我向下滚动时,它不会从暂停的地方恢复。我是怎么做到的?