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

news/2024/7/6 1:13:28

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);

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

在这里插入图片描述

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

image               
image


http://www.niftyadmin.cn/n/3649614.html

相关文章

angular id标记_使用传单在Angular中构建地图,第2部分:标记服务

angular id标记In my last post, we set up our environment to create a basic map using Leaflet and Angular. Let’s go a bit further now and add markers to our map. 在上一篇文章中 &#xff0c;我们设置了环境以使用Leaflet和Angular创建基本地图。 现在让我们再走一…

Java高新技术---反射动态代理

2015-04-25 一、反射 1、概述 JAVA反射机制是在运行状态中&#xff0c;对于任意一个类&#xff0c;都能够知道这个类的所有属性和方法&#xff1b;对于任意一个对象&#xff0c;都能够调用它的任意一个方法和属 性&#xff1b;这种动态获取的信息以及动态调用对象的方…

[xmlpull]XmlPull常见错误

[xmlpull]XmlPull常见错误编写者日期关键词郑昀ultrapower2005-9-28Xmlpull kxml java Xmlpull官方站点&#xff1a;http://www.xmlpull.org/优点&#xff1a;不必等整个文档解析完成&#xff0c;部分求值结果早就可以开始反馈给用户。What Is It?XmlPull project is dedicate…

C#实战系列—学生信息管理系统(一)项目展示

最近在整理自己电脑上的学习资料&#xff0c;突然发现大二时小组一起做的C#项目——学生信息管理系统。就想运行起来玩玩。可是现在主机里面都是一些开发Android和Java的软件。visual studio 2010也早就卸载了。不过想到我们开发的这个系统在Windows 10系统上的兼容性不太好。所…

在VS Code中使用ESLint进行整理和格式化

介绍 (Introduction) When writing JavaScript with an editor such as Visual Studio Code, there are a number of ways you can ensure your code is syntactically correct and in line with current best practices. For example, it is possible to integrate a linter s…

C#实战系列—学生信息管理系统(二)源码分析

对部分核心源代码进行分析&#xff0c;项目已开源&#xff0c;查看完整代码&#xff0c;文章底部有链接。 学生信息管理系统分为三个部分 项目展示 源码分析 项目打包 现在展示的是对原有系统进行二次开发的结果。为2.0版本。 一、界面设计 1、新建项目 新建项目的时候选择Wi…

如何使用MongoDB和Docker设置Flask

The author selected the Internet Archive to receive a donation as part of the Write for DOnations program. 作者选择了Internet存档作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) Developing web applications can become complex and ti…