教育系统APP(八)

用一切抱怨的时间去努力

项目“课程”的模块2

目录

  1. 学习目标
    1.1 掌握课程模块的开发,实现用VideoView完成视频的播放
    1.2 掌握课程模块的开发,实现用JSONObject完成Json数据的解析
    1.3 掌握课程模块的开发,实现用数据库保存播放记录

  2. 项目实操
    2.1 课程详情
    2.1.1 课程详情界面布局
    2.1.2 课程详情Item
    2.1.3 课程详情界面适配器和数据库
    2.1.4 课程详情界面逻辑代码

    2.2 视频播放
    2.2.1 视频播放布局
    2.2.2 视频播放逻辑代码

    2.3 播放记录
    2.3.1 播放记录布局
    2.3.2 播放记录item
    2.3.3 播放记录adapter
    2.3.4 播放记录逻辑代码

  3. 任务实施
    3.1 json解析
    3.2 播放网络视频

学习目标

1.1掌握课程模块的开发,实现用VideoView完成视频的播放
1.2掌握课程模块的开发,实现用JSONObject完成Json数据的解析
1.3掌握课程模块的开发,实现用数据库保存播放记录

课程模块主要用于展示课程中的视频信息,当点击课程列表时会跳转到课程详情界面,在该界面中可以播放相应章节的视频。同时为了方便用户查看已学习的视频,还在“我”的界面中添加了一个播放记录。本章将对课程界面进行详细讲解。

项目实操

课程详情
课程详情界面布局
把本章所需要的图片放入drawable文件夹中

36

在res包下新建一个资源包,命名为raw,放入视频文件video11.mp4

新建一个布局文件,命名为activity_video_list
课程详情布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/default_video_list_icon"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/video_list_intro_icon">
            <TextView
                android:id="@+id/tv_intro"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_centerVertical="true"
                android:background="#30B4FF"
                android:gravity="center"
                android:text="简 介"
                android:textColor="#FFFFFF"
                android:textSize="20sp"/>
        </RelativeLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="48dp"
            android:background="#C3C3C3"/>
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/video_list_intro_icon">
            <TextView
                android:id="@+id/tv_video"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_centerVertical="true"
                android:background="#FFFFFF"
                android:gravity="center"
                android:text="视 频"
                android:textColor="#000000"
                android:textSize="20sp"/>
        </RelativeLayout>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/lv_video_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:divider="#E4E4E4"
            android:dividerHeight="1dp"
            android:scrollbars="none"
            android:visibility="gone"/>
        <ScrollView
            android:id="@+id/sv_chapter_intro"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/tv_chapter_intro"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:lineSpacingMultiplier="1.5"
                    android:padding="10dp"
                    android:text="安卓简介"
                    android:textColor="@android:color/black"
                    android:textSize="14sp"/>
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>
</LinearLayout>

课程详情Item
课程详情界面里也有一个课程列表,这里我们用ListView就可以了。
新建一个布局文件,命名为video_list_item。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:paddingBottom="15dp"
    android:paddingTop="15dp">
    <ImageView
        android:id="@+id/iv_left_icon"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/course_bar_icon"/>
    <TextView
        android:id="@+id/tv_video_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:text="[第一节]Android简介"
        android:textColor="#333333"
        android:textSize="14sp"/>
</LinearLayout>

课程详情界面适配器和数据库
先写一个视频的属性类,命名为VideoBean

VideoBean.java

package cn.edu.gdmec.android.vicdemo.Bean;

public class VideoBean {
    public int chapterId;//章节Id
    public int videoId;//视频Id
    public String videoPath;//视频播放地址
    public String title;//章节标题
    public String secondTitle;//视频标题
}

写完属性类,我们来完善数据库的代码,为后面的播放记录做准备。
打开Sqlite包下的SQLiteHelper.java,补充视频表的代码。

SQLiteHelper.java

package cn.edu.gdmec.android.vicdemo.Sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper{
    private static final int DB_VERSION = 1;
    public static String DB_NAME = "bxg.db";
    public static final String U_USER_INFO = "userInfo";
    public static final String U_VIDEO_PLAY_LIST="videoplaylist";

    /*public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }*/
    public SQLiteHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        /**
         * 当这个SQLiteOpenHelper的子类类被实例化时会创建指定名的数据库,在onCreate中创建个人信息表
         * **/
        db.execSQL("CREATE TABLE IF NOT EXISTS " + U_USER_INFO + "( "
                + "_id  INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "userName VARCHAR, "
                + "nickName VARCHAR, "
                + "sex VARCHAR, "
                + "signature VARCHAR, "
                + "qq VARCHAR "
                + ")");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + U_VIDEO_PLAY_LIST + "( "
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "userName VARCHAR, " //用户名
                + "chapterId INT, " //章节
                + "videoId int, "       //小节id
                + "videoPath VARCHAR,"  //视频地址
                + "title VARCHAR,"//视频章节名称
                + "secondTitle VARCHAR"//视频名单
                + ")");
    }

    /**
     * 当数据库版本号增加才会调用此方法
     **/
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + U_USER_INFO);
        db.execSQL("DROP TABLE IF EXISTS " + U_VIDEO_PLAY_LIST);
        onCreate(db);
    }
}

到Utils包下的DBUtils.java中补充操作数据表的方法。

DBUtils.java

package cn.edu.gdmec.android.vicdemo.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Bean.UserBean;
import cn.edu.gdmec.android.vicdemo.Bean.VideoBean;
import cn.edu.gdmec.android.vicdemo.Sqlite.SQLiteHelper;

public class DBUtils {
    private static DBUtils instance = null;
    private static SQLiteHelper helper;
    private static SQLiteDatabase db;

    /**
     * 构造方法,只有当类被实例化时候调用
     * 实例化SQLiteHelper类,从中得到一个课读写的数据库
     **/
    public DBUtils(Context context) {
        helper = new SQLiteHelper(context);
        db = helper.getWritableDatabase();
    }

    /**
     * 得到这个类的实例
     **/
    public static DBUtils getInstance(Context context) {
        if (instance == null) {
            instance = new DBUtils(context);
        }
        return instance;
    }

    /**
     * 保存个人资料信息
     **/
    public void saveUserInfo(UserBean bean) {
        ContentValues cv = new ContentValues();
        cv.put("userName", bean.userName);
        cv.put("nickName", bean.nickName);
        cv.put("sex", bean.sex);
        cv.put("signature", bean.signature);
        cv.put("qq",bean.qq);
        //Convenience method for inserting a row into the database.
        //注意,我们是从数据库使用插入方法,传入表名和数据集完成插入
        db.insert(SQLiteHelper.U_USER_INFO, null, cv);
    }

    /**
     * 获取个人资料信息
     **/
    public UserBean getUserInfo(String userName) {
        String sql = "SELECT * FROM " + SQLiteHelper.U_USER_INFO + " WHERE userName=?";
        //?和下面数组内元素会逐个替换,可以多条件查询=?and =?
        //You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs.
        Cursor cursor = db.rawQuery(sql, new String[]{userName});
        UserBean bean = null;
        //Move the cursor to the next row.
        while (cursor.moveToNext()) {
            bean = new UserBean();
            //根据列索引获取对应的数值,因为这里查询结果只有一个,我们也不需要对模型UserBean进行修改,
            //直接将对应用户名的所有数据从表中动态赋值给bean
            bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
            bean.nickName = cursor.getString(cursor.getColumnIndex("nickName"));
            bean.sex = cursor.getString(cursor.getColumnIndex("sex"));
            bean.signature = cursor.getString(cursor.getColumnIndex("signature"));
            bean.qq = cursor.getString(cursor.getColumnIndex("qq"));
        }
        cursor.close();
        return bean;
    }

    /**
     * 修改个人资料信息,这里的key指代表字段,value表示数值
     **/
    public void updateUserInfo(String key, String value, String userName) {
        ContentValues cv = new ContentValues();
        cv.put(key, value);
        //Convenience method for updating rows in the database.
        db.update(SQLiteHelper.U_USER_INFO, cv, "userName=?", new String[]
                {userName});
    }
    public void saveVideoPlayList(VideoBean videoBean, String userName){
        if (hasVideoPlay(videoBean.chapterId, videoBean.videoId, userName)){
            boolean isDelete = delVideoPlay(videoBean.chapterId, videoBean.videoId, userName);
            if (!isDelete){
                return;
            }
        }
        ContentValues cv = new ContentValues();
        cv.put("userName", userName);
        cv.put("chapterId", videoBean.chapterId);
        cv.put("videoId", videoBean.videoId);
        cv.put("videoPath", videoBean.videoPath);
        cv.put("title", videoBean.title);
        cv.put("secondTitle", videoBean.secondTitle);
        db.insert(SQLiteHelper.U_VIDEO_PLAY_LIST, null, cv);
    }
    private boolean delVideoPlay(int chapterId, int videoId, String userName){
        boolean delSuccess = false;
        int row = db.delete(SQLiteHelper.U_VIDEO_PLAY_LIST,
                " chapterId=? AND videoId=? AND userName=?",
                new String[]{chapterId + "", videoId + "", userName});
        if (row > 0){
            delSuccess = true;
        }
        return delSuccess;
    }
    private boolean hasVideoPlay(int chapterId, int videoId, String userName){
        boolean hasVideo = false;
        String sql = "SELECT * FROM " + SQLiteHelper.U_VIDEO_PLAY_LIST +
                " WHERE chapterId=? AND videoId=? AND userName=?";
        Cursor cursor = db.rawQuery(sql, new String[]{chapterId + "", videoId + "", userName});
        if (cursor.moveToNext()){
            hasVideo = true;
        }
        cursor.close();
        return hasVideo;
    }
    public List<VideoBean> getVideoHistory(String s){
        String sql = "SELECT * FROM " + SQLiteHelper.U_VIDEO_PLAY_LIST + " WHERE userName=?";
        Cursor cursor = db.rawQuery(sql, new String[]{s});
        List<VideoBean> vbl = new ArrayList<>();
        VideoBean bean = null;
        while (cursor.moveToNext()){
            bean = new VideoBean();
            bean.chapterId = cursor.getInt(cursor.getColumnIndex("chapterId"));
            bean.videoId = cursor.getInt(cursor.getColumnIndex("videoId"));
            bean.videoPath = cursor.getString(cursor.getColumnIndex("videoPath"));
            bean.title = cursor.getString(cursor.getColumnIndex("title"));
            bean.secondTitle = cursor.getString(cursor.getColumnIndex("secondTitle"));
            vbl.add(bean);
            bean = null;
        }
        cursor.close();
        return vbl;
    }
}

适配器代码:
VideoListItemAdapter.java

package cn.edu.gdmec.android.vicdemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Activity.VideoPlayActivity;
import cn.edu.gdmec.android.vicdemo.Bean.VideoBean;
import cn.edu.gdmec.android.vicdemo.R;
import cn.edu.gdmec.android.vicdemo.utils.AnalysisUtils;
import cn.edu.gdmec.android.vicdemo.utils.DBUtils;

public class VideoListItemAdapter extends BaseAdapter {

    private List<VideoBean> objects = new ArrayList<VideoBean>();
    private int selectedPosition = -1;

    private DBUtils db;
    private Context context;
    private LayoutInflater layoutInflater;

    public VideoListItemAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        db = DBUtils.getInstance(context);
    }
    public void setData(List<VideoBean> vbl){
        this.objects = vbl;
        notifyDataSetChanged();
    }

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

    public void setSelectedPosition(int position){
        selectedPosition = position;
    }

    @Override
    public VideoBean getItem(int position) {
        return objects.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.video_list_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((VideoBean)getItem(position), (ViewHolder) convertView.getTag(), position, convertView);
        return convertView;
    }

    private void initializeViews(final VideoBean object, final ViewHolder holder, final int position, View convertView) {
        holder.ivLeftIcon.setImageResource(R.drawable.course_bar_icon);
        holder.tvVideoTitle.setTextColor(Color.parseColor("#333333"));
        if (object != null){
            holder.tvVideoTitle.setText(object.secondTitle);
            if (selectedPosition == position){
                holder.ivLeftIcon.setImageResource(R.drawable.course_intro_icon);;
                holder.tvVideoTitle.setTextColor(Color.parseColor("$009958"));
            }else {
                holder.ivLeftIcon.setImageResource(R.drawable.course_bar_icon);
                holder.tvVideoTitle.setTextColor(Color.parseColor("#333333"));
            }
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectedPosition(position);
                if (object != null){
                    String videoPath = object.videoPath;
                    notifyDataSetChanged();
                    if (TextUtils.isEmpty(videoPath)){
                        Toast.makeText(context, "本地没有此视频,暂无法播放", Toast.LENGTH_SHORT).show();
                    }else {
                        if (readLoginStatus()){
                            String userName = AnalysisUtils.readLoginUserName(context);
                            db.saveVideoPlayList(objects.get(position), userName);
                        }
                        //视频播放
                        Intent intent = new Intent(context, VideoPlayActivity.class);
                        intent.putExtra("videoPath", videoPath);
                        intent.putExtra("position", position);
                        ((Activity) context).startActivityForResult(intent, 1);
                    }
                }
            }
        });
    }
    private boolean readLoginStatus(){
        SharedPreferences sp = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        return isLogin;
    }

    protected class ViewHolder {
        private ImageView ivLeftIcon;
        private TextView tvVideoTitle;

        public ViewHolder(View view) {
            ivLeftIcon = (ImageView) view.findViewById(R.id.iv_left_icon);
            tvVideoTitle = (TextView) view.findViewById(R.id.tv_video_title);
        }
    }
}

课程详情界面逻辑代码
先把data.json文件放进assets文件夹。

VideoListActivity的逻辑代码

VideoListActivity.java

package cn.edu.gdmec.android.vicdemo.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Bean.VideoBean;
import cn.edu.gdmec.android.vicdemo.R;
import cn.edu.gdmec.android.vicdemo.adapter.VideoListItemAdapter;

public class VideoListActivity extends Activity{
    private TextView tvIntro;
    private TextView tvVideo;
    private ListView lvVideoList;
    private ScrollView svChapterIntro;
    private TextView tvChapterIntro;
    private List<VideoBean> videoList;
    private int chapterId;
    private String intro;
    private VideoListItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        chapterId = getIntent().getIntExtra("id", 0);
        intro = getIntent().getStringExtra("intro");
        initData();
        initView();
    }

    //把json文件转换为字符串
    private String read(InputStream is){
        BufferedReader reader = null;
        StringBuilder sb = null;
        String line = null;
        try{
            sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(is));
            while ((line = reader.readLine()) != null){
                sb.append(line);
                sb.append("\n");
            }
        }catch (IOException e){
            e.printStackTrace();
            return "";
        }finally {
            if (is != null){
                try{
                    is.close();
                    if (reader != null){
                        reader.close();
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    //解析json
    private void initData() {
        JSONArray jsonArray;
        try{
            InputStream is = getResources().getAssets().open("data.json");
            jsonArray = new JSONArray(read(is));
            videoList = new ArrayList<VideoBean>();
            for (int i = 0; i < jsonArray.length(); i++){
                VideoBean bean = new VideoBean();
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                if (jsonObj.getInt("chapterId") == chapterId){
                    bean.chapterId = jsonObj.getInt("chapterId");
                    bean.videoId = Integer.parseInt(jsonObj.getString("videoId"));
                    bean.title = jsonObj.getString("title");
                    bean.secondTitle = jsonObj.getString("secondTitle");
                    bean.videoPath = jsonObj.getString("videoPath");
                    videoList.add(bean);
                }
                bean = null;
            }
        }catch (IOException e){
            e.printStackTrace();
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    private void initView() {
        tvIntro = (TextView) findViewById(R.id.tv_intro);
        tvVideo = (TextView) findViewById(R.id.tv_video);
        lvVideoList = (ListView) findViewById(R.id.lv_video_list);
        svChapterIntro = (ScrollView) findViewById(R.id.sv_chapter_intro);
        tvChapterIntro = (TextView) findViewById(R.id.tv_chapter_intro);
        adapter = new VideoListItemAdapter(this);
        lvVideoList.setAdapter(adapter);
        adapter.setData(videoList);
        tvChapterIntro.setText(intro);
        tvIntro.setBackgroundColor(Color.parseColor("#30B4FF"));
        tvVideo.setBackgroundColor(Color.parseColor("#FFFFFF"));
        tvIntro.setTextColor(Color.parseColor("#FFFFFF"));
        tvVideo.setTextColor(Color.parseColor("#000000"));
        tvIntro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lvVideoList.setVisibility(View.GONE);
                svChapterIntro.setVisibility(View.VISIBLE);
                tvIntro.setBackgroundColor(Color.parseColor("#30B4FF"));
                tvVideo.setBackgroundColor(Color.parseColor("#FFFFFF"));
                tvIntro.setTextColor(Color.parseColor("#FFFFFF"));
                tvVideo.setTextColor(Color.parseColor("#000000"));
            }
        });
        tvVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lvVideoList.setVisibility(View.VISIBLE);
                svChapterIntro.setVisibility(View.GONE);
                tvIntro.setBackgroundColor(Color.parseColor("#FFFFFF"));
                tvVideo.setBackgroundColor(Color.parseColor("#30B4FF"));
                tvIntro.setTextColor(Color.parseColor("#000000"));
                tvVideo.setTextColor(Color.parseColor("#FFFFFF"));
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data != null){
            int position = data.getIntExtra("position", 0);
            adapter.setSelectedPosition(position);
            lvVideoList.setVisibility(View.VISIBLE);
            svChapterIntro.setVisibility(View.GONE);
            tvIntro.setBackgroundColor(Color.parseColor("#FFFFFF"));
            tvVideo.setBackgroundColor(Color.parseColor("#30B4FF"));
            tvIntro.setTextColor(Color.parseColor("#000000"));
            tvVideo.setTextColor(Color.parseColor("#FFFFFF"));
        }
    }
}

到CourseListItemAdapter.java中添加上跳转至视频详情页面的代码。
CourseListItemAdapter.java

package cn.edu.gdmec.android.vicdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Activity.VideoListActivity;
import cn.edu.gdmec.android.vicdemo.Bean.CourseBean;
import cn.edu.gdmec.android.vicdemo.R;

public class CourseListItemAdapter extends RecyclerView.Adapter<CourseListItemAdapter.ViewHolder> {

    private List<CourseBean> objects = new ArrayList<CourseBean>();
    private Context context;
    private LayoutInflater layoutInflater;

    public CourseListItemAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view=inflater.inflate(R.layout.course_list_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        initializeViews(objects.get(position), holder);
    }

    public void setData(List<CourseBean> objects){
        this.objects=objects;
    }

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

    @Override
    public int getItemCount() {
        return objects.size();
    }

    private void initializeViews(final CourseBean object, ViewHolder holder) {
        if (object != null){
            holder.tvCourseImgTitle.setText(object.imgTitle);
            holder.tvCourseTitle.setText(object.title);
            switch (object.id){
                case 1:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_1_icon);
                    break;
                case 2:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_2_icon);
                    break;
                case 3:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_3_icon);
                    break;
                case 4:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_4_icon);
                    break;
                case 5:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_5_icon);
                    break;
                case 6:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_6_icon);
                    break;
                case 7:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_7_icon);
                    break;
                case 8:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_8_icon);
                    break;
                case 9:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_9_icon);
                    break;
                case 10:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_10_icon);
                    break;
            }
            holder.ivCourseImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, VideoListActivity.class);
                    intent.putExtra("id", object.id);
                    intent.putExtra("intro", object.intro);
                    context.startActivity(intent);
                }
            });
        }
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivCourseImg;
        private TextView tvCourseImgTitle;
        private TextView tvCourseTitle;

        public ViewHolder(View view) {
            super(view);
            ivCourseImg = (ImageView) view.findViewById(R.id.iv_course_img);
            tvCourseImgTitle = (TextView) view.findViewById(R.id.tv_course_img_title);
            tvCourseTitle = (TextView) view.findViewById(R.id.tv_course_title);
        }
    }
}

再到AndroidManifest.xml中声明activity

视频播放
视频播放布局
新建布局文件,命名为activity_video_play。

activity_video_play.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

视频播放逻辑代码

VideoPlayActivity.java

package cn.edu.gdmec.android.vicdemo.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import cn.edu.gdmec.android.vicdemo.R;

public class VideoPlayActivity extends Activity {

    private VideoView videoView;
    private MediaController controller;
    private String videoPath;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        videoPath = getIntent().getStringExtra("videoPath");
        position = getIntent().getIntExtra("position", 0);
        videoView = findViewById(R.id.videoView);
        controller = new MediaController(this);
        videoView.setMediaController(controller);
        play();
    }

    private void play() {
        if(TextUtils.isEmpty(videoPath)){
            Toast.makeText(this,"本地没有此视频,暂无法播放", Toast.LENGTH_SHORT).show();
            return;
        }
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.video11;
        videoView.setVideoPath(uri);
        videoView.start();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        Intent data = new Intent();
        data.putExtra("position", position);
        setResult(RESULT_OK, data);
        return super.onKeyDown(keyCode, event);
    }
}

到VideoListItemAdapter.java里补上跳转视频播放页面的代码。
VideoListItemAdapter.java

//视频播放
Intent intent = new Intent(context, VideoPlayActivity.class);
intent.putExtra("videoPath", videoPath);
intent.putExtra("position", position);
((Activity) context).startActivityForResult(intent, 1);

到AndroidManifest.xml中声明activity

播放记录

播放记录布局
新建布局文件,命名为activity_play_history

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <include layout="@layout/main_title_bar"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:id="@+id/lv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#E4E4E4"
            android:dividerHeight="1dp"
            android:scrollbars="none"/>
        <TextView
            android:id="@+id/tv_none"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="暂无播放记录"
            android:textColor="@android:color/darker_gray"
            android:textSize="16sp"
            android:visibility="gone"/>
    </RelativeLayout>
</LinearLayout>

播放记录item
新建布局文件,命名为play_history_list_item
play_history_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:padding="10dp">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/iv_video_icon"
            android:layout_width="100dp"
            android:layout_height="75dp"
            android:src="@drawable/video_play_icon2"/>
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@android:drawable/ic_media_play"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="15dp"
        android:layout_gravity="center_vertical">
        <TextView
            android:id="@+id/tv_adapter_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="16sp"
            android:textColor="#333333"
            android:text="第1章 Android基础入门"
            android:gravity="center_vertical"/>
        <TextView
            android:layout_marginTop="4dp"
            android:id="@+id/tv_video_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="12sp"
            android:textColor="#a3a3a3"
            android:text="Android系统简介"
            android:gravity="center_vertical"/>
    </LinearLayout>
</LinearLayout>

播放记录adapter

PlayHistoryListItemAdapter.java

package cn.edu.gdmec.android.vicdemo.adapter;

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

import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Activity.VideoPlayActivity;
import cn.edu.gdmec.android.vicdemo.Bean.VideoBean;
import cn.edu.gdmec.android.vicdemo.R;

public class PlayHistoryListItemAdapter extends BaseAdapter {

    private List<VideoBean> objects = new ArrayList<VideoBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public PlayHistoryListItemAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<VideoBean> vbl){
        this.objects = vbl;
        notifyDataSetChanged();
    }

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

    @Override
    public VideoBean getItem(int position) {
        return objects.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.play_history_list_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((VideoBean)getItem(position), (ViewHolder) convertView.getTag(), convertView);
        return convertView;
    }

    private void initializeViews(final VideoBean object, ViewHolder holder, final View convertView) {
        if (object != null){
            holder.tvAdapterTitle.setText(object.title);
            holder.tvVideoTitle.setText(object.secondTitle);
            switch (object.chapterId){
                case 1:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon1);
                    break;
                case 2:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon2);
                    break;
                case 3:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon3);
                    break;
                case 4:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon4);
                    break;
                case 5:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon5);
                    break;
                case 6:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon6);
                    break;
                case 7:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon7);
                    break;
                case 8:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon8);
                    break;
                case 9:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon9);
                    break;
                case 10:
                    holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon10);
                    break;
                    default:
                        holder.ivVideoIcon.setImageResource(R.drawable.video_play_icon1);
                        break;
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (object == null){
                        return;
                    }
                    Intent intent = new Intent(context, VideoPlayActivity.class);
                    intent.putExtra("videoPath", object.videoPath);
                    context.startActivity(intent);
                }
            });
        }
    }

    protected class ViewHolder {
        private ImageView ivVideoIcon;
        private TextView tvAdapterTitle;
        private TextView tvVideoTitle;

        public ViewHolder(View view) {
            ivVideoIcon = (ImageView) view.findViewById(R.id.iv_video_icon);
            tvAdapterTitle = (TextView) view.findViewById(R.id.tv_adapter_title);
            tvVideoTitle = (TextView) view.findViewById(R.id.tv_video_title);
        }
    }
}

播放记录逻辑代码

PlayHistoryActivity.java

package cn.edu.gdmec.android.vicdemo.Activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.vicdemo.Bean.VideoBean;
import cn.edu.gdmec.android.vicdemo.R;
import cn.edu.gdmec.android.vicdemo.adapter.PlayHistoryListItemAdapter;
import cn.edu.gdmec.android.vicdemo.utils.AnalysisUtils;
import cn.edu.gdmec.android.vicdemo.utils.DBUtils;

public class PlayHistoryActivity extends Activity {

    private TextView tv_back;
    private TextView tv_main_title;
    private TextView tv_save;
    private RelativeLayout title_bar;
    private ListView lv_list;
    private TextView tv_none;
    private List<VideoBean> vbl;
    private DBUtils db;
    private PlayHistoryListItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_history);
        db = DBUtils.getInstance(this);
        vbl = new ArrayList<VideoBean>();
        vbl = db.getVideoHistory(AnalysisUtils.readLoginUserName(this));
        initView();
    }

    private void initView() {
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_save = findViewById(R.id.tv_save);
        title_bar = findViewById(R.id.title_bar);
        lv_list = findViewById(R.id.lv_list);
        tv_none = findViewById(R.id.tv_none);
        tv_main_title.setText("播放记录");
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        if (vbl.size() == 0){
            tv_none.setVisibility(View.VISIBLE);
        }
        adapter = new PlayHistoryListItemAdapter(this);
        adapter.setData(vbl);
        lv_list.setAdapter(adapter);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayHistoryActivity.this.fileList();
            }
        });
    }

}

MyinfoFragment.java里补上跳转视频播放页面的代码。

//跳转到播放记录页面
                    Intent intent = new Intent(getActivity(), PlayHistoryActivity.class);
                    getActivity().startActivity(intent);

到AndroidManifest.xml中声明activity
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.gdmec.android.vicdemo">

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".Activity.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--添加实现类-->
        <activity android:name=".Activity.MainActivity"></activity>
        <activity android:name=".Activity.RegisterActivity"></activity>
        <activity android:name=".Activity.LoginActivity"></activity>
        <activity android:name=".Activity.SettingActivity"></activity>
        <activity android:name=".Activity.ModifyPswActivity"></activity>
        <activity android:name=".Activity.FindPwdActivity"></activity>
        <activity android:name=".Activity.UserInfoActivity"></activity>
        <activity android:name=".Activity.ChangeUserInfoActivity"></activity>
        <activity android:name=".Activity.ExercisesDetailActivity"></activity>
        <activity android:name=".Activity.VideoListActivity"></activity>
        <activity android:name=".Activity.VideoPlayActivity"></activity>
        <activity android:name=".Activity.PlayHistoryActivity"></activity>
    </application>
</manifest>

任务实施

json解析
将res/assets包下的data.Json文件打开,把下面的json数据覆盖掉原来的数据,更改解析json数据的代码,把新的json数据解析出来并成功展示。

播放网络视频
修改VideoView控件和AndroidManifest.xml的代码,使其能成功播放下面json数据里的网络视频。

json数据:
[{
 "chapterId": 1,
 "data": [
  {
   "videoId": "1",
   "title": "第1章 Android 基础入门",
   "secondTitle": "迷情巧克力",
   "videoPath": "https://gslb.miaopai.com/stream/PaGWXOMYz-rMWm-r9a2MvILINHMc0YBXe2i-dg__.mp4?ssig=acb6880de7200e266864581d97c0654d&time_stamp=1524887992615&cookie_id=&vend=1&os=3&partner=1&platform=2&cookie_id=&refer=miaopai&scid=PaGWXOMYz-rMWm-r9a2MvILINHMc0YBXe2i-dg__"
  },
  {
   "videoId": "2",
   "title": "第1章 Android 基础入门",
   "secondTitle": "命运是什么",
   "videoPath": "https://gslb.miaopai.com/stream/KP7xXv0IllZsjnK1HjVlkbGfb5yWj7mPbaEOmQ__.mp4?ssig=7d445a22609119e57b50c8fa9a84a11b&time_stamp=1524888071281&cookie_id=&vend=1&os=3&partner=1&platform=2&cookie_id=&refer=miaopai&scid=KP7xXv0IllZsjnK1HjVlkbGfb5yWj7mPbaEOmQ__"
  }
 ]
},
 {
  "chapterId": 2,
  "data": [
   {
    "videoId": "1",
    "title": "第2章 Android UI开发",
    "secondTitle": "水银为什么是液体",
    "videoPath": "https://gslb.miaopai.com/stream/cnngO-Co7rFlVPzkS7qqoaxR7OFH72uvYDDiew__.mp4?ssig=b91b143afbaadb8472b57b17e0c213e5&time_stamp=1524888092840&cookie_id=&vend=1&os=3&partner=1&platform=2&cookie_id=&refer=miaopai&scid=cnngO-Co7rFlVPzkS7qqoaxR7OFH72uvYDDiew__"
   },
   {
    "videoId": "2",
    "title": "第2章 Android UI开发",
    "secondTitle": "汽车的历史",
    "videoPath": "https://gslb.miaopai.com/stream/6S23U5PMRtOQzu99NeITYztd0xtKFW2xDYxwXQ__.mp4?ssig=afa07a802aca690154953446fc5de4d4&time_stamp=1524888118545&cookie_id=&vend=1&os=3&partner=1&platform=2&cookie_id=&refer=miaopai&scid=6S23U5PMRtOQzu99NeITYztd0xtKFW2xDYxwXQ__"
   }
  ]
 },
 {
  "chapterId": 3,
  "data": [
   {
    "videoId": "1",
    "title": "第3章 Activity",
    "secondTitle": "汽车的历史",
    "videoPath": "https://gslb.miaopai.com/stream/6S23U5PMRtOQzu99NeITYztd0xtKFW2xDYxwXQ__.mp4?ssig=afa07a802aca690154953446fc5de4d4&time_stamp=1524888118545&cookie_id=&vend=1&os=3&partner=1&platform=2&cookie_id=&refer=miaopai&scid=6S23U5PMRtOQzu99NeITYztd0xtKFW2xDYxwXQ__"
   },
   {
    "chapterId": 3,
    "videoId": "2",
    "title": "第3章 Activity",
    "secondTitle": "Activity中的数据传递",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 4,
  "data": [
   {
    "videoId": "1",
    "title": "第4章 数据存储",
    "secondTitle": "文件存储",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第4章 数据存储",
    "secondTitle": "XML序列化与SharedPreferences",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 5,
  "data": [
   {
    "videoId": "1",
    "title": "第5章 SQLite 数据库",
    "secondTitle": "SQLite数据库的使用",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第5章 SQLite 数据库",
    "secondTitle": "ListView控件的使用",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 6,
  "data": [
   {
    "videoId": "1",
    "title": "第6章 广播接收者",
    "secondTitle": "内容提供者简介",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第6章 广播接收者",
    "secondTitle": "内容提供者的访问",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 7,
  "data": [
   {
    "videoId": "1",
    "title": "第7章 服务",
    "secondTitle": "广播接收者与自定义广播",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第7章 服务",
    "secondTitle": "广播类型与常用的广播接收者",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 8,
  "data": [
   {
    "videoId": "1",
    "title": "第8章 内容提供者",
    "secondTitle": "服务的创建与生命周期",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第8章 内容提供者",
    "secondTitle": "服务的启动方式",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 9,
  "data": [
   {
    "videoId": "1",
    "title": "第9章 网络编程",
    "secondTitle": "访问网络",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第9章 网络编程",
    "secondTitle": "数据的提交方式",
    "videoPath": ""
   }
  ]
 },
 {
  "chapterId": 10,
  "data": [
   {
    "videoId": "1",
    "title": "第10章 高级编程",
    "secondTitle": "图形图像处理与动画",
    "videoPath": ""
   },
   {
    "videoId": "2",
    "title": "第10章 高级编程",
    "secondTitle": "多媒体、与Fragment",
    "videoPath": ""
   }
  ]
 }
]

总结 完

解决方法

VideoListActivity

//解析json
    private void initData() {
        //JSONArray jsonArray;
        JSONArray jsonArray,jsonArray1;
        List<String> list = new ArrayList<String>();
        List<VideoBean> list1 = new ArrayList<VideoBean>();

        try{
            InputStream is = getResources().getAssets().open("data.json");
            jsonArray = new JSONArray(read(is));
            videoList = new ArrayList<VideoBean>();
            for (int i = 0; i < jsonArray.length(); i++){
                VideoBean bean = new VideoBean();
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                if (jsonObj.getInt("chapterId") == chapterId){
                    bean.chapterId = jsonObj.getInt("chapterId");

                    /*bean.videoId = Integer.parseInt(jsonObj.getString("videoId"));
                    bean.title = jsonObj.getString("title");
                    bean.secondTitle = jsonObj.getString("secondTitle");
                    bean.videoPath = jsonObj.getString("videoPath");
                    videoList.add(bean);*/
                    String ss =jsonObj.getString("data");
                    jsonArray1 = new JSONArray(ss);
                    for (int j=0;j<jsonArray1.length();j++){
                        JSONObject jsonObject = (JSONObject) jsonArray1.get(j);

                        Iterator<String> iterator = jsonObject.keys();
                        while (iterator.hasNext()){
                            String key = iterator.next();
                            String value = jsonObject.getString(key);
                            list.add(value);
                        }
                        bean.videoId = Integer.parseInt(list.get(0));
                        bean.title = list.get(1);
                        bean.secondTitle = list.get(2);
                        bean.videoPath = list.get(3);
                        videoList.add(bean);
                        bean = new VideoBean();
                        list.clear();
                        Log.i("Ss",videoList.toString());
                    }

                }
                bean = null;
            }
        }catch (IOException e){
            e.printStackTrace();
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

VideoPlayActivity

package cn.edu.gdmec.android.vicdemo.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import cn.edu.gdmec.android.vicdemo.R;

public class VideoPlayActivity extends Activity {

    private VideoView videoView;
    private MediaController controller;
    private String videoPath;
    private int position;////传递视频详情界面点击的视频位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置界面全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video_play);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //获取从播放记录界面传递过来的视频地址
        videoPath = getIntent().getStringExtra("videoPath");
        position = getIntent().getIntExtra("position", 0);

        videoView = findViewById(R.id.videoView);
        controller = new MediaController(this);
        videoView.setMediaController(controller);
        play();
    }

    private void play() {
        if(TextUtils.isEmpty(videoPath)){
            Toast.makeText(this,"本地没有此视频,暂无法播放", Toast.LENGTH_SHORT).show();
            return;
        }
        //String uri = "android.resource://" + getPackageName() + "/" + R.raw.video11;
        Log.i("Video",videoPath);
        String uri = videoPath;
        Uri uri1 = Uri.parse(uri);
        videoView.setVideoURI(uri1);
        //videoView.setVideoPath(uri);
        videoView.start();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        //把视频详情界面传递过来的被点击视频的位置传递回去
        Intent data = new Intent();
        data.putExtra("position", position);
        setResult(RESULT_OK, data);
        return super.onKeyDown(keyCode, event);
    }
}

大功告成!


文章作者: Jeskson
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jeskson !
评论
 上一篇
腾讯智慧校园开发平台1 腾讯智慧校园开发平台1
腾讯智慧校园开发平台1 OpenAPI参数: openAppID(或devCode、devType、keyId)、timestamp、sign、objectid、objType openAppID(第三方应用ID)+AppSecret(
下一篇 
教育系统APP(七) 教育系统APP(七)
用一切抱怨的时间去努力 ## 回顾上一期任务 判断题数点击完选项之后,底部的文本控件要变换刚刚点击的题目的题号。注意:在里面用到adapter与Activity的信息回调,具体过程参考 保存答题状态当5道题都回答过一遍后,点击返回习题
2018-04-27
  目录