【Android studio】初学安卓简单视频播放。催眠app制作。[催眠手册]

作品目的

最近漫画看多了,刚好学一些简单的安卓操作,做了一个简单的视频播放器。
就一个套壳的小玩具。大佬别骂了别骂了。
目的:
1.做一个简单登录界面。
2.可以播放视频。
3.改变app名称和图像。
ps:账号密码都是:jiulili。我之前的马甲,不喜欢的兄弟可以在mainactivity中自己改。

知识了解

播放视频与播放音频相比,播放视频需要使用视觉控件将影像展示出来。Android系统中的VideoView控件就是播放视频用的,借助它可以完成一个简易的视频播放器。VideoView控件提供了一些用于控制视频播放的方法。
本app中只是播放了一次视频,没有循环播放。播完即停。
在这里插入图片描述

代码展示

做完后在手机上展示长这样。
在这里插入图片描述

在这里插入图片描述
有两个activity。一个负责登录,登录成功后跳转到另外一个activity中。直接播放视频。
废话不多说,以下是代码源码:
mainactivity:

package com.example.lovee;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    
    
    private EditText et_username;
    private EditText et_password;
    private Button bt_log;
    private Button bt_bos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通过资源标识获得控件实例
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_log = (Button) findViewById(R.id.bt_log);
        bt_bos = (Button) findViewById(R.id.bt_bos);


        //给登录按钮注册监听器,实现监听器接口,编写事件
        bt_log.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                //获取用户输入的数据
                String strUsername = et_username.getText().toString();
                String strPassword = et_password.getText().toString();

                //判断用户名和密码是否正确(为可以进行测试,将用户名和密码都定义为admin)
                if (strUsername.equals("jiulili") && strPassword.equals("jiulili")) {
    
    
                    Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this,Show.class);
                    // 存储当前用户名,用于工具界面显示信息
                    intent.putExtra("username", strUsername);
                    startActivity(intent);
                } else if (strUsername.equals("") || strPassword.equals("")) {
    
    
                    // 用户名、密码不能为空
                    Toast.makeText(MainActivity.this, "用户名/密码不能为空!", Toast.LENGTH_SHORT).show();
                } else {
    
    
                    Toast.makeText(MainActivity.this, "登陆失败!用户名或密码错误!", Toast.LENGTH_SHORT).show();
                }

            }
        });
        //给取消按钮注册监听器,实现监听器接口,编写事件
        bt_bos.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                finish();
            }
        });


    }

}

showactivity:

package com.example.lovee;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.net.Uri;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Show extends AppCompatActivity {
    
    
    private TextView welcome;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        // 获取所有对象
        welcome = (TextView) findViewById(R.id.welcome);
        // 通过
        final Intent intent = getIntent();
        intent.getStringExtra("username");
        welcome.setText("欢迎您" + intent.getStringExtra("username"));


        VideoView videoView =(VideoView) findViewById(R.id.videoview);
        String url="android.resource://"+ getPackageName()+"/"+R.raw.video1;
        Uri uri = Uri.parse(url); //字符串ur1解析成Uri
        videoView.setVideoURI(uri); //设置videoview的播放资源

        videoView.start();//播放视频


    }

}

接下来是页面布局的文件:
activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gr"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C H A R M"
        android:textSize="35dp"
        android:layout_marginTop="200dp"
        android:textColor="#FF6347"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户"
            android:textSize="20dp"
            android:textColor="#F08080"/>

        <EditText
            android:id="@+id/et_username"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="输入用户名"
            android:layout_marginLeft="10dp"
            android:textColorHint="#FFC0CB"
            android:singleLine="true"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="20dp"
            android:textColor="#F08080"/>

        <EditText
            android:id="@+id/et_password"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="输入密码"
            android:layout_marginLeft="10dp"
            android:textColorHint="#FFC0CB"
            android:inputType="textPassword"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_people"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="催眠对象"
            android:textSize="20dp"
            android:textColor="#F08080"/>

        <EditText
            android:id="@+id/et_people"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="输入您要催眠的对象"
            android:layout_marginLeft="10dp"
            android:textColorHint="#FFC0CB"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp"
        android:gravity="center_horizontal">

        <Button
            android:id="@+id/bt_log"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fbc2eb"
            android:text="登录"
            android:textSize="18dp"/>

        <Button
            android:id="@+id/bt_bos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:background="#fbc2eb"
            android:text="取消"
            android:textSize="18dp"/>

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_marginTop="200dp"
        android:textColor="#76D7C4"
        android:textSize="20dp"
        android:text="本软件由'知识为甚不进脑'赞助出品。"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

activityshow:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Show">

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="80dp"
        android:text=""
        android:layout_alignParentBottom="true"
        android:textColor="#f1e9f6"
        android:textSize="20sp" />

</RelativeLayout>

视频长这样:
在这里插入图片描述
截了个图直接录了一个十秒的视频。
在这里插入图片描述
需要注意的是,res里面需要新建一个文件夹“raw”放置mp4文件。
在androidmanifest中,需要多加两句权限。不然视频可能无法播放。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

修改app的图像和名字在这里:
在这里插入图片描述
在这里插入图片描述
图片全部来自网上,只是自己娱乐使用,侵删。

有不太懂安卓但是想试试的兄弟也可以直接拿我网盘的apk文件。安装就可以用,账号:jiulili。密码:jiulili
链接:https://pan.baidu.com/s/1x4cvJLogqlkWyAnCjnkt3w?pwd=0505
提取码:0505

感谢支持。就是自己突发奇想简单做出来的东西,高兴就做了,勿喷。

猜你喜欢

转载自blog.csdn.net/gelly_/article/details/132866145