Android基础——本地视频播放器

上次写的是网络视频播放的,这次写一个本地视频播放的,和本地音频播放很相似,代码如下:

//读写SD卡权限

   
   

//首先写一个实体类,代码如下(两个参数分别为:基本的路径和视频标题):

public class LocalVideoBean {
    private String path;
    private String title;

    public LocalVideoBean() {
    }

    public LocalVideoBean(String path, String title) {
        this.path = path;
        this.title = title;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "LocalVideoBean{" +
                "path='" + path + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}

//主界面代码:

import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.jj.weektwo.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LocalVideoActivity extends AppCompatActivity {
    private ListView lv;
    private List mlist=new ArrayList<>();
    private LocalVideoAdapter adapter;
    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_video);
        lv=(ListView)findViewById(R.id.video_lv);//初始化控件

        adapter=new LocalVideoAdapter(mlist,this);
        lv.setAdapter(adapter);//设置适配器
         
        LoadLocalData();//这个方法为集合添加数据
        mediaPlayer=new MediaPlayer();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {//listView设置监听事件
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                LocalVideoBean bean=mlist.get(i);
                stopVoide();
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(bean.getPath());
                    playVoide();//开始视频播放
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        });
    }

    private void LoadLocalData() {
        ContentResolver resolver=getContentResolver();//四大组件中的内容提供者
        Uri uri= MediaStore.Video.Media.EXTERNAL_CONTENT_URI;//本地视频uri路径
        Cursor cursor=resolver.query(uri,null,null,null,MediaStore.Video.Media.DEFAULT_SORT_ORDER);
        while (cursor.moveToNext()){
            String title=cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
            String path=cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));

           LocalVideoBean bean=new LocalVideoBean(path,title);
            mlist.add(bean);//为list添加数据
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    protected void onDestroy() {//当界面销毁时,关闭视频播放
        super.onDestroy();
        stopVoide();
    }

    private void playVoide() {
        if (mediaPlayer!=null&&!mediaPlayer.isPlaying()){
            try {
                mediaPlayer.prepare();//准备
                mediaPlayer.start();//开始
            } catch (IOException e) {
                 e.printStackTrace();
            }
        }
    }

    private void stopVoide() {
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.pause();//暂停
            mediaPlayer.seekTo(0);//进度归0
            mediaPlayer.stop();//停止
        }
    }
}

//适配器代码

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.jj.weektwo.R;

import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by jj on 2018/1/6.
 */

public class LocalVideoAdapter extends BaseAdapter {
    private List mlist;
    private Context context;
   private LayoutInflater inflater;
    public LocalVideoAdapter(List mlist, Context context) {
        this.mlist = mlist;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            holder=new ViewHolder();
            view=inflater.inflate(R.layout.item_video,viewGroup,false);
            holder.playerStandard=(JZVideoPlayerStandard)view.findViewById(R.id.video_JZ);
            view.setTag(holder);
        }else {
            holder=(ViewHolder)view.getTag();
        }
        LocalVideoBean bean=mlist.get(i);
        holder.playerStandard.setUp(bean.getPath(), JZVideoPlayer.SCREEN_WINDOW_LIST,bean.getTitle());
        return view;
    }

    class ViewHolder{
        JZVideoPlayerStandard playerStandard;
    }
}



你可能感兴趣的:(Android,学习笔记)