Android项目实战系列—基于博学谷(七)课程模块(下)

image

由于这个模块内容较多,分为上、中、下 三篇博客分别来讲述,请耐心阅读。


课程模块分为四个部分


课程模块(下)主要讲述视频播放和播放记录两个部分

一、视频播放

1、视频播放界面

(1)、创建视频播放界面

com.boxuegu.activity包中创建一个Java类,命名为VideoPlayActivity。在res/layout文件夹下创建布局文件,命名为activity_video_play

(2)、界面代码——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="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

2、视频播放界面逻辑代码

(1)、在res文件夹下创建raw文件夹。用来存放视频。放入测试视频video11.mp4

(2)。逻辑代码——VideoPlayActivity.java

package com.boxuegu.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import com.boxuegu.R;

public class VideoPlayActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        videoPath = getIntent().getStringExtra("videoPath");
        position = getIntent().getIntExtra("position",0);
        init();

    }

    private void init(){
        videoView = (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);

    }
}

3、修改界面代码

(1)、找到VideoListActivity.java文件,在注释//跳转到视频播放界面下方添加如下代码:

Intent intent=new Intent(VideoListActivity.this,VideoPlayActivity.class);
intent.putExtra("videoPath",videoPath);
intent.putExtra("position",position);
startActivityForResult(intent,1);

(1)、找到VideoListActivity.java文件,添加以下方法:

 @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);
            //
            lv_video_list.setVisibility(View.VISIBLE);
            sv_chapter_intro.setVisibility(View.GONE);
            tv_intro.setBackgroundColor(Color.parseColor("#FFFFFF"));
            tv_video.setBackgroundColor(Color.parseColor("#30B4FF"));
            tv_intro.setTextColor(Color.parseColor("#000000"));
            tv_video.setTextColor(Color.parseColor("#FFFFFF"));
        }
    }

二、播放记录

1、播放记录界面

(1)、创建播放记录界面

com.boxuegu.activity包中创建一个Java类,命名为PlayHistoryActivity,创建布局文件activity_play_history。导入所需界面图片 video_play_icon1.pngvideo_play_icon2.pngvideo_play_icon3.pngvideo_play_icon4.pngvideo_play_icon5.pngvideo_play_icon6.pngvideo_play_icon7.pngvideo_play_icon8.pngvideo_play_icon9.pngvideo_play_icon10.png

(2)界面代码——activity_play_history.xml

<?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="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/lv_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#E4E4E4"
            android:dividerHeight="1dp"
            android:scrollbars="none"/>
        <TextView
            android:id="@+id/tv_none"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="暂无播放记录"
            android:textColor="@android:color/darker_gray"
            android:textSize="16sp"
            android:visibility="gone"/>
    </RelativeLayout>
</LinearLayout>

2、播放记录界面Item

(1)、创建播放记录界面Item,在res/layout文件夹下创建一个布局文件,命名为play_history_list_item

(2)、界面代码play_history_list_item.xml

<?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: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:src="@android:drawable/ic_media_play"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_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="fill_parent"
            android:textSize="16sp"
            android:textColor="#333333"
            android:gravity="center_vertical"/>
        <TextView
            android:layout_marginTop="4dp"
            android:id="@+id/tv_video_title"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            android:textColor="#a3a3a3"
            android:gravity="center_vertical"/>
    </LinearLayout>
</LinearLayout>

3、播放记录界面Adapter

com.boxuegu.adapter包下创建一个Java类,命名为PlayHistoryAdapter。代码如下:

package com.boxuegu.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.List;
import com.boxuegu.R;
import com.boxuegu.activity.VideoPlayActivity;
import com.boxuegu.bean.VideoBean;

//播放历史
public class PlayHistoryAdapter extends BaseAdapter {
    private Context mContext;
    private List<VideoBean> vbl;
    public PlayHistoryAdapter(Context context){
        this.mContext = context;
    }

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

    @Override
    public int getCount() {
        return vbl == null ? 0 : vbl.size();
    }

    @Override
    public VideoBean getItem(int i) {
        return vbl == null ? null : vbl.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if (view == null){
            vh = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.play_history_list_item,null
            );
            vh.tv_title = (TextView) view.findViewById(R.id.tv_adapter_title);
            vh.tv_video_title = (TextView) view.findViewById(R.id.tv_video_title);
            vh.iv_icon = (ImageView) view.findViewById(R.id.iv_video_icon);
            view.setTag(vh);
        }else{
            vh = (ViewHolder) view.getTag();
        }
        final VideoBean bean = getItem(i);
        if (bean!=null){
            vh.tv_title.setText(bean.title);
            vh.tv_video_title.setText(bean.secondTitle);
            switch (bean.chapterId){
                case 1:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon1);
                    break;
                case 2:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon2);
                    break;
                case 3:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon3);
                    break;
                case 4:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon4);
                    break;
                case 5:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon5);
                    break;
                case 6:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon6);
                    break;
                case 7:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon7);
                    break;
                case 8:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon8);
                    break;
                case 9:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon9);
                    break;
                case 10:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon10);
                    break;
                default:
                    vh.iv_icon.setImageResource(R.drawable.video_play_icon1);
                    break;
            }
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean==null) return;
                Intent intent = new Intent(mContext, VideoPlayActivity.class);
                intent.putExtra("videoPath",bean.videoPath);
                mContext.startActivity(intent);
            }
        });
        return view;
    }
    class ViewHolder{
        public TextView tv_title,tv_video_title;
        public ImageView iv_icon;

    }
}

4、播放记录界面逻辑代码——PlayHistoryActivity.java

package com.boxuegu.activity;

import android.content.pm.ActivityInfo;
import com.boxuegu.R;
import com.boxuegu.adapter.PlayHistoryAdapter;
import com.boxuegu.bean.VideoBean;
import com.boxuegu.utils.AnalysisUtils;
import com.boxuegu.utils.DBUtils;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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


public class PlayHistoryActivity extends AppCompatActivity {
    private TextView tv_main_title, tv_back, tv_none;
    private RelativeLayout rl_title_bar;
    private ListView lv_list;
    private PlayHistoryAdapter adapter;
    private List<VideoBean> vbl;
    private DBUtils db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_history);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        db=DBUtils.getInstance(this);
        vbl=new ArrayList<VideoBean>();
        vbl=db.getVideoHistory(AnalysisUtils.readLoginUserName(this));
        init();
    }

    private void init(){
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("播放记录");
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4ff"));
        tv_back = (TextView) findViewById(R.id.tv_back);
        lv_list = (ListView) findViewById(R.id.lv_list);
        tv_none = (TextView) findViewById(R.id.tv_none);
        if (vbl.size()==0){
            tv_none.setVisibility(View.VISIBLE);
        }
        adapter=new PlayHistoryAdapter(this);
        adapter.setData(vbl);
        lv_list.setAdapter(adapter);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayHistoryActivity.this.finish();
                PlayHistoryActivity.this.finish();
            }
        });
    }
}

5、修改界面代码

(1)、找到DBUtils.java,添加如下代码:

public ArrayList<VideoBean> getVideoHistory(String userName){
        String sql="SELECT * FROM " + SQLiteHelper.U_VIDEO_PLAY_LIST + " WHERE userName = ?";
        Cursor cursor = db.rawQuery(sql,new String[]{userName});
        ArrayList<VideoBean> vbl = new ArrayList<VideoBean  >();
        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;
    }

(2)、找到MyInfoView.java文件,在注释//跳转到播放记录界面下方添加如下代码:

Intent intent = new Intent(mContext, PlayHistoryActivity.class);
mContext.startActivity(intent);

三、运行效果

image


现在这个项目的基本功能都已经完成。看着自己有道云笔记里面的文章,满满的成就感。按照项目综述,后面还会有创新点打包上线的模块。由于这个项目也是移动高级开发的期末项目。所以创新点暂时不更新。期末作品上交以后会公开创新模块(7月中旬才会更新)。敬请期待!

image

Android项目实战系列—基于博学谷 开源地址

image               
image

赞(6) 打赏
未经允许不得转载:微笑涛声 » Android项目实战系列—基于博学谷(七)课程模块(下)
分享到:
标签:

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏