gpt4 book ai didi

android - 如何在滚动 ListView 时逐个播放视频和播放/暂停

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:12 24 4
gpt4 key购买 nike

我想播放 frontView 上的视频,如何检测 listView 中的 View 并在 getView 时播放并在向上滚动时暂停。如何做到这一点?

这是我的代码。

public class MainActivity extends Activity {
ListView listview;
CustomListAdapter adapter;
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 CustomListAdapter(MainActivity.this, videoLIst);
// Set video link (mp4 format )
listview.setAdapter(adapter);
listview.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub

int firstVisibleRow = listview.getFirstVisiblePosition();
int lastVisibleRow = listview.getLastVisiblePosition();

for(int i=firstVisibleRow;i<=lastVisibleRow;i++)
{
//Write your code here(allocation/deallocation/store in array etc.)
System.out.println(i + "=" + listview.getItemAtPosition(i));
currentItem = (int) listview.getItemAtPosition(i);
}
}
});


}

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.activity_main, null);

VideoView video = (VideoView)convertView.findViewById(R.id.VideoView);
TextView txvvideo =(TextView)convertView.findViewById(R.id.txvposition);

txvvideo.setText("Video"+position);

mediaController.setAnchorView(video);
Log.d("video url inadapter:", ""+videoLIst.toString());
Uri uri = Uri.parse(videoList.get(currentItem));
video.setMediaController(mediaController);
video.setVideoURI(uri);
video.start();
int pos;
pos=video.getCurrentPosition();
// if(pos == currentItem)
// {
// video.pause();
// }
// video.setMediaController(mediaController);
// video.setVideoURI(Uri.parse(videoList.get(position)));
// video.start();
return convertView;
}

}

}

当我向上或向下滚动视频时,它在 listview 中不可见时,我想停止视频。

最佳答案

创建一个 RecyclerListener并将其设置在 ListView 上:

    listView.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
videoView.stopPlayback();
}
});

当 View 滚动到屏幕外并变得“不可见”时,ListView 将回收它,您将调用 onMovedToScrapHeap()。

关于android - 如何在滚动 ListView 时逐个播放视频和播放/暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31554602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com