Android:本地视频播放器开发 — 搜索本地视频(2)

1、首先介绍布局代码,主布局代码只含有一个LIstView  --jie_video.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.     <ListView  
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="match_parent"  
  8.         android:layout_alignParentBottom="true"  
  9.         android:id="@+id/jievideolistfile"  
  10.     />  
  11. </RelativeLayout>  

2、下一个布局就是listView的子项的布局

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.     <ImageView   
  6.         android:layout_width="120dp"  
  7.         android:layout_height="80dp"  
  8.         android:id="@+id/video_img"  
  9.         android:contentDescription="@string/cont"  
  10.         />  
  11.     <RelativeLayout   
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent"  
  14.         android:layout_toRightOf="@id/video_img"  
  15.         android:layout_alignBottom="@id/video_img"  
  16.         >  
  17.         <TextView   
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:id="@+id/video_title"  
  21.             android:gravity="center"  
  22.             android:layout_marginTop="5dp"  
  23.             android:text="@string/title"  
  24.         />  
  25.           
  26.         <TextView   
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:id="@+id/video_time"  
  30.             android:gravity="center"  
  31.             android:layout_alignParentBottom="true"  
  32.             android:layout_marginBottom="5dp"  
  33.             android:text="@string/time"  
  34.         />  
  35.     </RelativeLayout>  
  36. </RelativeLayout>  


3、布局都写好了,然后就是Activity的编写,这里涉及到视频的缩略图的显示,所以要用到异步加载功能

JieVideo.java

[java]  view plain copy
  1. package com.zhangjie.graduation.videopalyer;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.zhangjie.graduation.videopalyer.component.JieVideoListViewAdapter;  
  6. import com.zhangjie.graduation.videopalyer.component.LoadedImage;  
  7. import com.zhangjie.graduation.videopalyer.videofile.AbstructProvider;  
  8. import com.zhangjie.graduation.videopalyer.videofile.Video;  
  9. import com.zhangjie.graduation.videopalyer.videofile.VideoProvider;  
  10.   
  11. import android.app.Activity;  
  12. import android.content.Intent;  
  13. import android.graphics.Bitmap;  
  14. import android.graphics.drawable.BitmapDrawable;  
  15. import android.media.ThumbnailUtils;  
  16. import android.os.AsyncTask;  
  17. import android.os.Bundle;  
  18. import android.provider.MediaStore.Video.Thumbnails;  
  19. import android.view.View;  
  20. import android.widget.AdapterView;  
  21. import android.widget.ImageView;  
  22. import android.widget.ListView;  
  23. import android.widget.AdapterView.OnItemClickListener;  
  24.   
  25. public class JieVideo extends Activity{  
  26.   
  27.     public JieVideo instance = null;  
  28.     ListView mJieVideoListView;  
  29.     JieVideoListViewAdapter mJieVideoListViewAdapter;  
  30.     List<Video> listVideos;  
  31.     int videoSize;  
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.jie_video);  
  36.         instance = this;  
  37.         AbstructProvider provider = new VideoProvider(instance);  
  38.         listVideos = provider.getList();  
  39.         videoSize = listVideos.size();  
  40.         mJieVideoListViewAdapter = new JieVideoListViewAdapter(this, listVideos);  
  41.         mJieVideoListView = (ListView)findViewById(R.id.jievideolistfile);  
  42.         mJieVideoListView.setAdapter(mJieVideoListViewAdapter);  
  43.         mJieVideoListView.setOnItemClickListener(new OnItemClickListener() {  
  44.             @Override  
  45.             public void onItemClick(AdapterView<?> parent, View view, int position,  
  46.                     long id) {  
  47.                 Intent intent = new Intent();  
  48.                 intent.setClass(JieVideo.this, JieVideoPlayer.class);  
  49.                 Bundle bundle = new Bundle();  
  50.                 bundle.putSerializable("video", listVideos.get(position));  
  51.                 intent.putExtras(bundle);  
  52.                 startActivity(intent);  
  53.             }  
  54.         });  
  55.         loadImages();  
  56.     }  
  57.     /** 
  58.      * Load images. 
  59.      */  
  60.     private void loadImages() {  
  61.         final Object data = getLastNonConfigurationInstance();  
  62.         if (data == null) {  
  63.             new LoadImagesFromSDCard().execute();  
  64.         } else {  
  65.             final LoadedImage[] photos = (LoadedImage[]) data;  
  66.             if (photos.length == 0) {  
  67.                 new LoadImagesFromSDCard().execute();  
  68.             }  
  69.             for (LoadedImage photo : photos) {  
  70.                 addImage(photo);  
  71.             }  
  72.         }  
  73.     }  
  74.     private void addImage(LoadedImage... value) {  
  75.         for (LoadedImage image : value) {  
  76.             mJieVideoListViewAdapter.addPhoto(image);  
  77.             mJieVideoListViewAdapter.notifyDataSetChanged();  
  78.         }  
  79.     }  
  80.     @Override  
  81.     public Object onRetainNonConfigurationInstance() {  
  82.         final ListView grid = mJieVideoListView;  
  83.         final int count = grid.getChildCount();  
  84.         final LoadedImage[] list = new LoadedImage[count];  
  85.   
  86.         for (int i = 0; i < count; i++) {  
  87.             final ImageView v = (ImageView) grid.getChildAt(i);  
  88.             list[i] = new LoadedImage(  
  89.                     ((BitmapDrawable) v.getDrawable()).getBitmap());  
  90.         }  
  91.   
  92.         return list;  
  93.     }  
  94.     /**  
  95.         * 获取视频缩略图  
  96.         * @param videoPath  
  97.         * @param width  
  98.         * @param height  
  99.         * @param kind  
  100.         * @return  
  101.         */    
  102.        private Bitmap getVideoThumbnail(String videoPath, int width , int height, int kind){    
  103.         Bitmap bitmap = null;    
  104.         bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);    
  105.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);    
  106.         return bitmap;    
  107.        }    
  108.   
  109.     class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {  
  110.         @Override  
  111.         protected Object doInBackground(Object... params) {  
  112.             Bitmap bitmap = null;  
  113.             for (int i = 0; i < videoSize; i++) {  
  114.                 bitmap = getVideoThumbnail(listVideos.get(i).getPath(), 120120, Thumbnails.MINI_KIND);  
  115.                 if (bitmap != null) {  
  116.                     publishProgress(new LoadedImage(bitmap));  
  117.                 }  
  118.             }  
  119.             return null;  
  120.         }  
  121.           
  122.         @Override  
  123.         public void onProgressUpdate(LoadedImage... value) {  
  124.             addImage(value);  
  125.         }  
  126.     }  
  127. }  

4、上面还涉及一个就是LIstView的适配器,这里是继承BaseAdapter。

JieVideoListViewAdapter.java

[java]  view plain copy
  1. package com.zhangjie.graduation.videopalyer.component;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.zhangjie.graduation.videopalyer.R;  
  7. import com.zhangjie.graduation.videopalyer.videofile.Video;  
  8.   
  9. import android.content.Context;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16.   
  17. public class JieVideoListViewAdapter extends BaseAdapter{  
  18.   
  19.     List<Video> listVideos;  
  20.     int local_postion = 0;  
  21.     boolean imageChage = false;  
  22.     private LayoutInflater mLayoutInflater;  
  23.     private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();  
  24.     public JieVideoListViewAdapter(Context context, List<Video> listVideos){  
  25.         mLayoutInflater = LayoutInflater.from(context);  
  26.         this.listVideos = listVideos;  
  27.     }  
  28.     @Override  
  29.     public int getCount() {  
  30.         return photos.size();  
  31.     }  
  32.     public void addPhoto(LoadedImage image){  
  33.         photos.add(image);  
  34.     }  
  35.     @Override  
  36.     public Object getItem(int position) {  
  37.         return position;  
  38.     }  
  39.       
  40.     @Override  
  41.     public long getItemId(int position) {  
  42.         return position;  
  43.     }  
  44.   
  45.     @Override  
  46.     public View getView(int position, View convertView, ViewGroup parent) {  
  47.         ViewHolder holder = null;  
  48.         if (convertView == null) {  
  49.             holder = new ViewHolder();  
  50.             convertView = mLayoutInflater.inflate(R.layout.video_list_view, null);  
  51.             holder.img = (ImageView)convertView.findViewById(R.id.video_img);  
  52.             holder.title = (TextView)convertView.findViewById(R.id.video_title);  
  53.             holder.time = (TextView)convertView.findViewById(R.id.video_time);  
  54.             convertView.setTag(holder);  
  55.         }else {  
  56.             holder = (ViewHolder)convertView.getTag();  
  57.         }  
  58.             holder.title.setText(listVideos.get(position).getTitle());//ms  
  59.             long min = listVideos.get(position).getDuration() /1000 / 60;  
  60.             long sec = listVideos.get(position).getDuration() /1000 % 60;  
  61.             holder.time.setText(min+" : "+sec);  
  62.             holder.img.setImageBitmap(photos.get(position).getBitmap());  
  63.           
  64.         return convertView;  
  65.     }  
  66.   
  67.     public final class ViewHolder{  
  68.         public ImageView img;  
  69.         public TextView title;  
  70.         public TextView time;  
  71.     }  
  72. }  


5、还有一个在JieVideo类中使用了一个LoadedImage的类,它的代码如下:

[java]  view plain copy
  1. package com.zhangjie.graduation.videopalyer.component;  
  2.   
  3. import android.graphics.Bitmap;  
  4.   
  5.     public class LoadedImage {  
  6.         Bitmap mBitmap;  
  7.   
  8.     public LoadedImage(Bitmap bitmap) {  
  9.         mBitmap = bitmap;  
  10.     }  
  11.   
  12.     public Bitmap getBitmap() {  
  13.         return mBitmap;  
  14.     }  
  15. }  


那就看看最终的实现效果吧:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值