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

来源:互联网 发布:网络学校国家承认吗 编辑:程序博客网 时间:2024/05/14 08:57

 这一章的主要内容是搜索手机本地视频,添加到ListView列表里,每一个表项含有这个视频的缩略图,视频的播放时间,视频的标题,在搜索本地视频(1)中我们先制作搜索功能。

Video.java--视频相关的属性类

[java] view plaincopy
  1. package com.zhangjie.graduation.videopalyer.videofile;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import com.zhangjie.graduation.videopalyer.component.LoadedImage;  
  6.   
  7. public class Video implements Serializable{  
  8.     /** 
  9.      *  
  10.      */  
  11.     private static final long serialVersionUID = -7920222595800367956L;  
  12.     private int id;  
  13.     private String title;  
  14.     private String album;  
  15.     private String artist;  
  16.     private String displayName;  
  17.     private String mimeType;  
  18.     private String path;  
  19.     private long size;  
  20.     private long duration;  
  21.     private LoadedImage image;  
  22.   
  23.     /** 
  24.      *  
  25.      */  
  26.     public Video() {  
  27.         super();  
  28.     }  
  29.   
  30.     /** 
  31.      * @param id 
  32.      * @param title 
  33.      * @param album 
  34.      * @param artist 
  35.      * @param displayName 
  36.      * @param mimeType 
  37.      * @param data 
  38.      * @param size 
  39.      * @param duration 
  40.      */  
  41.     public Video(int id, String title, String album, String artist,  
  42.             String displayName, String mimeType, String path, long size,  
  43.             long duration) {  
  44.         super();  
  45.         this.id = id;  
  46.         this.title = title;  
  47.         this.album = album;  
  48.         this.artist = artist;  
  49.         this.displayName = displayName;  
  50.         this.mimeType = mimeType;  
  51.         this.path = path;  
  52.         this.size = size;  
  53.         this.duration = duration;  
  54.     }  
  55.   
  56.     public int getId() {  
  57.         return id;  
  58.     }  
  59.   
  60.     public void setId(int id) {  
  61.         this.id = id;  
  62.     }  
  63.   
  64.     public String getTitle() {  
  65.         return title;  
  66.     }  
  67.   
  68.     public void setTitle(String title) {  
  69.         this.title = title;  
  70.     }  
  71.   
  72.     public String getAlbum() {  
  73.         return album;  
  74.     }  
  75.   
  76.     public void setAlbum(String album) {  
  77.         this.album = album;  
  78.     }  
  79.   
  80.     public String getArtist() {  
  81.         return artist;  
  82.     }  
  83.   
  84.     public void setArtist(String artist) {  
  85.         this.artist = artist;  
  86.     }  
  87.   
  88.     public String getDisplayName() {  
  89.         return displayName;  
  90.     }  
  91.   
  92.     public void setDisplayName(String displayName) {  
  93.         this.displayName = displayName;  
  94.     }  
  95.   
  96.     public String getMimeType() {  
  97.         return mimeType;  
  98.     }  
  99.   
  100.     public void setMimeType(String mimeType) {  
  101.         this.mimeType = mimeType;  
  102.     }  
  103.   
  104.     public String getPath() {  
  105.         return path;  
  106.     }  
  107.   
  108.     public void setPath(String path) {  
  109.         this.path = path;  
  110.     }  
  111.   
  112.     public long getSize() {  
  113.         return size;  
  114.     }  
  115.   
  116.     public void setSize(long size) {  
  117.         this.size = size;  
  118.     }  
  119.   
  120.     public long getDuration() {  
  121.         return duration;  
  122.     }  
  123.   
  124.     public void setDuration(long duration) {  
  125.         this.duration = duration;  
  126.     }  
  127.       
  128.     public LoadedImage getImage(){  
  129.         return image;  
  130. <pre class="java" name="code">     
  131.     }  
  132.       
  133.     public void setImage(LoadedImage image){  
  134.         this.image = image;  
  135.     }  
  136.   
  137. }

  138. AbstructProvider.java ---一个接口,来获取搜索的视频的一个集合
  139. package com.zhangjie.graduation.videopalyer.videofile;  
  140.   
  141. import java.util.List;  
  142.   
  143. public interface AbstructProvider {  
  144.       
  145.     public List<Video> getList();  
  146.       
  147. }
  148.  
  149. VideoProvider.java--- 实现AbstructProvider接口,通过cursor来搜索视频的相关信息 
  150. <pre class="java" name="code">package com.zhangjie.graduation.videopalyer.videofile;  
  151.   
  152. import java.util.ArrayList;  
  153. import java.util.List;  
  154.   
  155. import android.content.Context;  
  156. import android.database.Cursor;  
  157. import android.provider.MediaStore;  
  158.   
  159. public class VideoProvider implements AbstructProvider {  
  160.     private Context context;  
  161.       
  162.     public VideoProvider(Context context) {  
  163.         this.context = context;  
  164.     }  
  165.       
  166.     @Override  
  167.     public List<Video> getList() {  
  168.         List<Video> list = null;  
  169.         if (context != null) {  
  170.             Cursor cursor = context.getContentResolver().query(  
  171.                     MediaStore.Video.Media.EXTERNAL_CONTENT_URI, nullnull,  
  172.                     nullnull);  
  173.             if (cursor != null) {  
  174.                 list = new ArrayList<Video>();  
  175.                 while (cursor.moveToNext()) {  
  176.                     int id = cursor.getInt(cursor  
  177.                             .getColumnIndexOrThrow(MediaStore.Video.Media._ID));  
  178.                     String title = cursor  
  179.                             .getString(cursor  
  180.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));  
  181.                     String album = cursor  
  182.                             .getString(cursor  
  183.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));  
  184.                     String artist = cursor  
  185.                             .getString(cursor  
  186.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));  
  187.                     String displayName = cursor  
  188.                             .getString(cursor  
  189.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));  
  190.                     String mimeType = cursor  
  191.                             .getString(cursor  
  192.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));  
  193.                     String path = cursor  
  194.                             .getString(cursor  
  195.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));  
  196.                     long duration = cursor  
  197.                             .getInt(cursor  
  198.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));  
  199.                     long size = cursor  
  200.                             .getLong(cursor  
  201.                                     .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));  
  202.                     Video video = new Video(id, title, album, artist, displayName, mimeType, path, size, duration);  
  203.                     list.add(video);  
  204.                 }  
  205.                 cursor.close();  
  206.             }  
  207.         }  
  208.         return list;  
  209.     }  
  210.   
  211. }
  212. 最后在主类中使用如下代码来获取最终得到的视频相关信息集合  
  213. AbstructProvider provider = new VideoProvider(this);  
  214. List<Video>   listVideos = provider.getList();
  215. 在上面的listVideos包含了本地所有的视频的相关信息,下一章将会用到listVideos数据。 

0 0
原创粉丝点击