Android本地視頻播放器開發--搜尋本地視頻(1)

來源:互聯網
上載者:User

   這一章的主要內容是搜尋手機本地視頻,添加到ListView列表裡,每一個表項含有這個視頻的縮圖,視頻的播放時間,視頻的標題,在搜尋本地視頻(1)中我們先製作搜尋功能。

Video.java--視頻相關的屬性類

[java]
package com.zhangjie.graduation.videopalyer.videofile; 
 
import java.io.Serializable; 
 
import com.zhangjie.graduation.videopalyer.component.LoadedImage; 
 
public class Video implements Serializable{ 
    /**
     * 
     */ 
    private static final long serialVersionUID = -7920222595800367956L; 
    private int id; 
    private String title; 
    private String album; 
    private String artist; 
    private String displayName; 
    private String mimeType; 
    private String path; 
    private long size; 
    private long duration; 
    private LoadedImage image; 
 
    /**
     * 
     */ 
    public Video() { 
        super(); 
    } 
 
    /**
     * @param id
     * @param title
     * @param album
     * @param artist
     * @param displayName
     * @param mimeType
     * @param data
     * @param size
     * @param duration
     */ 
    public Video(int id, String title, String album, String artist, 
            String displayName, String mimeType, String path, long size, 
            long duration) { 
        super(); 
        this.id = id; 
        this.title = title; 
        this.album = album; 
        this.artist = artist; 
        this.displayName = displayName; 
        this.mimeType = mimeType; 
        this.path = path; 
        this.size = size; 
        this.duration = duration; 
    } 
 
    public int getId() { 
        return id; 
    } 
 
    public void setId(int id) { 
        this.id = id; 
    } 
 
    public String getTitle() { 
        return title; 
    } 
 
    public void setTitle(String title) { 
        this.title = title; 
    } 
 
    public String getAlbum() { 
        return album; 
    } 
 
    public void setAlbum(String album) { 
        this.album = album; 
    } 
 
    public String getArtist() { 
        return artist; 
    } 
 
    public void setArtist(String artist) { 
        this.artist = artist; 
    } 
 
    public String getDisplayName() { 
        return displayName; 
    } 
 
    public void setDisplayName(String displayName) { 
        this.displayName = displayName; 
    } 
 
    public String getMimeType() { 
        return mimeType; 
    } 
 
    public void setMimeType(String mimeType) { 
        this.mimeType = mimeType; 
    } 
 
    public String getPath() { 
        return path; 
    } 
 
    public void setPath(String path) { 
        this.path = path; 
    } 
 
    public long getSize() { 
        return size; 
    } 
 
    public void setSize(long size) { 
        this.size = size; 
    } 
 
    public long getDuration() { 
        return duration; 
    } 
 
    public void setDuration(long duration) { 
        this.duration = duration; 
    } 
     
    public LoadedImage getImage(){ 
        return image; 
<PRE class=java name="code">    
    } 
     
    public void setImage(LoadedImage image){ 
        this.image = image; 
    } 
 
}</PRE> 
<PRE></PRE> 
<P>AbstructProvider.java ---一個介面,來擷取搜尋的視頻的一個集合</P> 
<PRE class=java name="code">package com.zhangjie.graduation.videopalyer.videofile; 
 
import java.util.List; 
 
public interface AbstructProvider { 
     
    public List<Video> getList(); 
     
}</PRE> 
<P><BR> 
VideoProvider.java--- 實現AbstructProvider介面,通過cursor來搜尋視頻的相關資訊</P> 
<PRE class=java name="code">package com.zhangjie.graduation.videopalyer.videofile; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.content.Context; 
import android.database.Cursor; 
import android.provider.MediaStore; 
 
public class VideoProvider implements AbstructProvider { 
    private Context context; 
     
    public VideoProvider(Context context) { 
        this.context = context; 
    } 
     
    @Override 
    public List<Video> getList() { 
        List<Video> list = null; 
        if (context != null) { 
            Cursor cursor = context.getContentResolver().query( 
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, 
                    null, null); 
            if (cursor != null) { 
                list = new ArrayList<Video>(); 
                while (cursor.moveToNext()) { 
                    int id = cursor.getInt(cursor 
                            .getColumnIndexOrThrow(MediaStore.Video.Media._ID)); 
                    String title = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); 
                    String album = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM)); 
                    String artist = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST)); 
                    String displayName = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)); 
                    String mimeType = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); 
                    String path = cursor 
                            .getString(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); 
                    long duration = cursor 
                            .getInt(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); 
                    long size = cursor 
                            .getLong(cursor 
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); 
                    Video video = new Video(id, title, album, artist, displayName, mimeType, path, size, duration); 
                    list.add(video); 
                } 
                cursor.close(); 
            } 
        } 
        return list; 
    } 
 
}</PRE> 
<P><BR> 
最後在主類中使用如下代碼來擷取最終得到的視頻相關資訊集合</P> 
<PRE class=java name="code">AbstructProvider provider = new VideoProvider(this); 
List<Video>   listVideos = provider.getList();</PRE> 
<P>在上面的listVideos包含了本地所有的視頻的相關資訊,下一章將會用到listVideos資料。<BR> 
</P> 
<PRE></PRE> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.