ListView自定义适配器 - 添加视频持续时间

时间:2017-06-14 14:16:43

标签: android listview

到目前为止,我已经实现了包含2个视频的视频视图,我可以设置数据库中的所有其他文本视图(标题,上传的用户,观看次数等)但我无法设置持续时间,因为我只是计算这个从数据库中获取视频网址后,但在获取视频时间后,会发生以下两种情况之一:

  • 1)持续时间仅更新列表中的最后一个textview项目
  • 2)当我将setText移出get duration方法时,我得到一个IndexOutOfBoundException。

我的代码如下:

            @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            ViewHolder_video holder;

            View row=convertView;
            if(row==null)
            {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.activity_video_single, parent, false);      
            }

            holder = new ViewHolder_video();                
            holder.ibProfilePicture = (ImageButton) row.findViewById(R.id.ibProfilePicture);

            TextView tvID = (TextView) row.findViewById(R.id.tvID);
            VideoView vv = (VideoView) row.findViewById(R.id.vv);
            TextView tvTitle = (TextView) row.findViewById(R.id.tvTitle);
            tvDuration = (TextView) row.findViewById(R.id.tvDuration);
            TextView tvUser = (TextView) row.findViewById(R.id.tvUser);
            TextView tvViews = (TextView) row.findViewById(R.id.tvViews);
            tvDate = (TextView) row.findViewById(R.id.tvDate);
            ImageButton ibProfilePicture = (ImageButton) row.findViewById(R.id.ibProfilePicture);

            tvID.setText(arrID.get(position));

            //VideoView URL
            vv.setVideoURI(Uri.parse(arrVideoURL.get(position)));
            vv.seekTo(5000);  
            vv.pause();

            //VideoView Duration
            vv.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {

                    //get video duration
                    int duration=mp.getDuration()/1000;
                    int hours = duration / 3600; 
                    int minutes = (duration / 60) - (hours * 60);
                    int seconds = duration - (hours * 3600) - (minutes * 60) ;

                    video_length = String.format("%d:%02d:%02d", hours, minutes, seconds);

                    if (hours == 0)
                    {
                        video_length = String.format("%02d:%02d", minutes, seconds); 
                    }

                    System.out.println("0--> video_length: "+video_length + " "+position); // I get my TWO video durations as position 0 and position 1
                    arrDuration.add(video_length); // I add my durations to an array (List<String> arrDuration)

                    tvDuration.setText(arrDuration.get(position)); // <-- setting the text here only sets the duration to the last item only
                }
            });


            //tvDuration.setText(arrDuration.get(position));    // <-- setting the text here returns error: IndexOutOgBoundException

            tvTitle.setText(arrTitle.get(position));
            tvUser.setText(arrUser.get(position));
            tvViews.setText(arrViews.get(position));
            tvDate.setText(arrDate.get(position));

            Picasso.with(context).load(arrProfilePicture.get(position)).resize(250, 250)
            .transform(new CircleTransform()).placeholder(R.drawable.ic_launcher).error(R.drawable.error).into(ibProfilePicture);   

            holder.ibProfilePicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "go to user profile: "+arrUser.get(position), Toast.LENGTH_SHORT).show();
                }
             });

            return row;
        }

当我使用其中任何一个时,请注意2 tvDuration.setText(arrDuration.get(position)),无效。

1 个答案:

答案 0 :(得分:0)

当您使用VideoView时,它会调用MediaPlayer,在您的代码中,它会在适配器中提供VideoView的最后一项,这就是您获得IndexOutOfBoundException的原因,因为onPrepare回调获取最后一项(position == lastIndex)和 当您为集合arrDuration.add(video_length)添加持续时间时,它将被添加到第一个位置。

逐步查看本教程https://medium.com/@v.danylo/implementing-video-playback-in-a-scrolled-list-listview-recyclerview-d04bc2148429