获得网络视屏的第一张帧图片

//------------baseadapter适配器中的---getView方法中------------------------------------------

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        ShiPin shiPin=shiPins.get(position);
        if (convertView==null) {
            convertView=View.inflate(context, R.layout.list_item, null);
            holder=new ViewHolder();
            holder.img=(ImageView) convertView.findViewById(R.id.iv_img);
            holder.username=(TextView) convertView.findViewById(R.id.tv_shipin_name);
            holder.videotime=(TextView) convertView.findViewById(R.id.tv_shipin_time);
            convertView.setTag(holder);                                
        }
        else{
            holder=(ViewHolder) convertView.getTag();
        }
        
        holder.username.setText(shiPin.getUsername());
        holder.videotime.setText(shiPin.getVideotime());
        
        //视频的网路地址   URL
        final String urls = "http://101.200.142.201:8080/VideoPlay/video" + shiPin.getVideourl();
        Log.i("111", urls);
        //使用异步                                                              位图
        new AsyncTask<String, Integer, Bitmap>(){

            @Override
            protected Bitmap doInBackground(String... params) {
                
                
                Bitmap bitmap = createVideoThumbnail(params[0],100,90);

                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap s) {
                super.onPostExecute(s);
                //设置图片
                holder.img.setImageBitmap(s);
            }
            //视频的网络地址
        }.execute(urls);

 

    return convertView;
    }

 

 

//-----------------另写一个方法----------------------------------

private Bitmap createVideoThumbnail(String url, int width, int height) {
        Bitmap bitmap = null;
        
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                retriever.setDataSource(url, new HashMap<String, String>());
            } else {
                retriever.setDataSource(url);
            }
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
            // Assume this is a corrupt video file.
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                // Ignore failures while cleaning up.
            }
        }
        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

posted on 2016-08-19 16:38  巫山老妖  阅读(226)  评论(0编辑  收藏  举报