列表视图中的视频视图每次滚动列表时都会停止

时间:2014-07-15 08:33:56

标签: android

我在listview中使用视频。但每次滚动列表时,应用程序崩溃(给出windowManager坏标记异常)或者没有响应,播放的视频也会停止。 如何在整个视图被粉碎时暂停视频。我不是在谈论滚动监听器我希望在整个视频视图滚动时暂停视频。 这是我的代码: -

public class Virtual_open_house_videos_activity extends Activity{
    TextView video_uploading_date,text_address;
    ListView list;
    String date,address;
    VideoView video;
    ArrayList<String>videos_list=new ArrayList<String>();
    ArrayList<Boolean> playingStatus;
    ArrayList<VideoView> mediaController;
    ImageView img_play_vid=null,img_dislike_vid=null,back_button_01;
    Custom custom_adapter=new Custom();
    String getvideo;
    MediaController media_Controller;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_virtual_open_house_videos);

        init();
        getVal();
        setVal();
        setListener();

    }
    void setListener(){
        back_button_01.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });

    }
    void init()
    {   
        video=(VideoView)findViewById(R.id.property_video_listview);
        back_button_01=(ImageView)findViewById(R.id.back_button_01);
        video_uploading_date=(TextView)findViewById(R.id.video_uploading_date);
        list=(ListView)findViewById(R.id.videos_list);
        img_play_vid=(ImageView)findViewById(R.id.img_play_vid);
        img_dislike_vid=(ImageView)findViewById(R.id.img_dislike_vid);
        playingStatus=new ArrayList<Boolean>();
        mediaController=new ArrayList<VideoView>();
        text_address=(TextView)findViewById(R.id.address_text);
        list.setAdapter(custom_adapter);

    }
    void getVal()
    {
        Intent intent=getIntent();
        date=intent.getExtras().getString("date");
        address=intent.getExtras().getString("address");
        Log.v("adress check",address);
        videos_list=intent.getStringArrayListExtra("video");
        Log.v("video list check", videos_list+"");

    }
    void setVal(){
        video_uploading_date.setText(date);
        videos_list.add(address);
        //al.addAll(videos_list);

    }
    class Custom extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return videos_list.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return videos_list.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(final int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            Log.v("entering adapter", "adapter entered correctly");
            View view=getLayoutInflater().inflate(R.layout.layout_inflate_virtual_video, null);
            final ImageView img_play_vid=(ImageView)view.findViewById(R.id.img_play_vid);
            final ImageView img_dislike_vid=(ImageView)view.findViewById(R.id.img_dislike_vid);
            TextView porp_address=(TextView)view.findViewById(R.id.address_text);
            porp_address.setText(address);
            getvideo=videos_list.get(arg0);
            video=(VideoView)view.findViewById(R.id.property_video_listview);
            Log.v("address check", address);

            /*view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(Virtual_open_house_videos_activity.this,Fsbo_video_play_activity.class);
                intent.putExtra("video", videos_list.get(arg0));
                startActivity(intent);
            }
        });*/
            playVid();
            img_play_vid.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Log.v("reaching code", "code reached");
                    if(playingStatus.get(arg0)==false){
                        //property_video.start();   
                        Log.v("playing status at postion "+arg0,""+playingStatus.get(arg0));
                        mediaController.get(arg0).start();
                        playingStatus.set(arg0, true);
                    }

                    else{
                        Log.v("playing status at postion "+arg0,""+playingStatus.get(arg0));
                        mediaController.get(arg0).pause();;

                        playingStatus.set(arg0, false);
                    }

                }
            });
            return view;    



    }
    public void playVid() {
        // TODO Auto-generated method stub
        Log.v("function called", "crashing here");
        MediaController mc=new MediaController(getApplicationContext());
        video.setMediaController(new MediaController(getApplicationContext()));
        mc.setMediaPlayer(video);
        mc.setAnchorView(video);
        Log.v("getting video", getvideo);
        Uri video_uri = Uri.parse(getvideo);
        video.setVideoURI(video_uri);
        playingStatus.add(false);
        Log.v("playing status", playingStatus+"");
        mediaController.add(video);
    }
    }
}

1 个答案:

答案 0 :(得分:0)

我在这里看到的问题是你的getView方法每次应该回收旧视图时都会重新创建视图,导致旧的媒体播放器在被销毁时继续播放或崩溃。

相反,您应该使用 ViewHolder 技术来正确回收您的视图,并且可能会在getView中检索到旧的媒体播放器实例后停止播放视频。

有关详情,请参阅thisthis