我想播放frontView上的视频,如何在listView
中检测视图,在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
中隐藏视频时停止播放。
答案 0 :(得分:2)
创建RecyclerListener并在ListView上设置:
listView.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
videoView.stopPlayback();
}
});
当视图在屏幕外滚动并变为&#34;隐藏&#34;时,ListView将回收它,并且您将调用onMovedToScrapHeap()。
答案 1 :(得分:0)
我从上面的代码开始,然后进行了一些更改:
public class MainActivity extends Activity {
ListView listview;
CustomListAdapter adapter;
public int mCurrentItem;
public static ArrayList<String> sVideoList;
static
{
sVideoList = new ArrayList<String>();
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
sVideoList.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
}
public static MediaController mediaController ;
boolean pauseOnScroll = true;
boolean pauseOnFling = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(MainActivity.this, sVideoList);
// Set video link (mp4 format )
listview.setAdapter(adapter);
listview.setRecyclerListener(new AbsListView.RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
// Safety net
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
videoView.stopPlayback();
}
});
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
private int mFirstVisibleRow = -1;
private int mActiveItem = -1;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int firstVisibleRow = listview.getFirstVisiblePosition();
if( mFirstVisibleRow != firstVisibleRow )
{
mFirstVisibleRow = firstVisibleRow;
// Cancel the video of the previous active item
VideoView prevActiveVideoView = getVideoViewForRow(mActiveItem);
if( prevActiveVideoView != null )
{
prevActiveVideoView.pause();
}
// Start the video of the new active item
mActiveItem = mFirstVisibleRow + 1;
VideoView newActiveVideoView = getVideoViewForRow(mActiveItem);
if(newActiveVideoView != null)
{
newActiveVideoView.start();
}
}
}
private VideoView getVideoViewForRow(int row)
{
int firstVisibleRow = listview.getFirstVisiblePosition();
View rowView = listview.getChildAt(row-firstVisibleRow);
return (rowView == null) ? null : (VideoView)rowView.findViewById(R.id.VideoView);
}
});
}
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.video_list_item, null);
VideoView videoView = (VideoView)convertView.findViewById(R.id.VideoView);
TextView videoTextView = (TextView)convertView.findViewById(R.id.txvposition);
// We setup the video here but we don't start it until the view is scrolled to
videoTextView.setText("Video " + position);
Uri uri = Uri.parse(videoList.get(position));
videoView.setVideoURI(uri);
return convertView;
}
}
}
和video_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:id="@+id/txvposition"
android:layout_width="match_parent"
android:layout_height="80dp" />
</LinearLayout>
您不必在创建视图时启动视频,而是等待它开始运行直到它变为活动状态。一次只允许一个VideoView变为活动状态,当它停止活动时,它会暂停。目前它的编码使得屏幕上的第二个可见VideoView处于活动状态。
这不是完美的代码。即,现在它已实现,列表视图中的第一个VideoView将永远不会播放其视频。但是,它确实解决了上述问题,并将帮助您进一步迭代。
答案 2 :(得分:0)