視頻播放器项目功能需求
1 项目概述
1.1 项目描述
視頻播放器(以及視頻管理系統)是一款由java(Swing,安卓和数据库)做的手机视频播放器,可以看手机本地的各种视频,通过按钮进入手机视频管理器。
1.2 项目需求
实现手机的视频播放以及视频的管理(二者是分开的,视频播放器和视频管理里系统)。
手机视频播放器用安卓来做,视频管理系统用Swing和数据库制作。
1.2.1 基本功能
初始生成一个登陆界面,进入登录注册。
注册之后进入视频播放界面。
用户可以通过HOME键打开手机本地视频。
1.2.2 扩展功能
拖动进度条
调节亮度
调节音量
2 项目知识点覆盖
本项目功能实现涉及并覆盖如下知识点:
Swing图形用户界面。
实现与数据库的交互。
Android多媒体视频播放。
这个视频播放器是用Android studio写的,环境配置的话,自己弄好就行了= =,太久之前写的我有些记不住了。

Global类:
package example.video3;
/**
- Created by Lenovo on 2019/5/30.
*/
public class Global {
public static Global i = new Global();
public String username = ""; // 当前用户的名字
}
MainActivity类:
package example.video3;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.VideoView;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity//去頭部!!!头部窗口AppCompatActivity
//我设置的一些图标模块在AppCompatActivity里,如果用Activity,界面的一些图标就会失效,例如播放图标,功能没有消失,但图标失踪
{
final String TAG = "测试";
final int REQ_OPEN_FILE = 101;
Intent intent;//Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,
// Intent不仅可用于应用程序之间,也可用于应用程序内部的activity, service和broadcast receiver之间的交互。Intent这个英语单词的本意是“目的、意向、意图”。
//Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,
// Android会根据意愿的内容选择适当的组件来响应。
VideoView videoView; // 播放器控件
SeekBar videoBar; // 可拖放视频进度条
SeekBar voiceBar; //音量进度条
SeekBar lightBar; //亮度进度条
View controlBar;//控制面板,暂停,播放,进度
View rotateBar;//控制面板,横屏,竖屏(未实现),分享(实现)
View titleBar;//返回上一界面,控制亮度,音量
private AudioManager mAudioManager;
//AudioManager类位于android.Media 包中,该类提供访问控制音量和钤声模式的操作。
private int maxVoice;
private int currentVoice;
Handler msgHandler;//Handler是Android SDK来处理异步消息的核心类。
//子线程与主线程通过Handler来进行通信。子线程可以通过Handler来通知主线程进行UI更新。
Timer timer;
//Timer,定时器,功能是在指定的时间间隔内反复触发指定窗口的定时器事件
TimerTask timerTask;
//Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。
// TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。
@Override
protected void onCreate(Bundle savedInstanceState)
{
// 从字面上看savedInstanceState,是保存实例状态的。实际上,savedInstanceState也就是保存Activity的状态的。
// 那么,savedInstanceState中的状态数据是从何处而来的呢?下面我们介绍Activity的另一个方法saveInstanceState。
// onsaveInstanceState方法是用来保存Activity的状态的。当一个Activity在生命周期结束前,会调用该方法保存状态。
// 这个方法有一个参数名称与onCreate方法参数名称相同。
//requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE );//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,//设置标题
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);//保存状态
setContentView(R.layout.activity_main);
//R.layout.main是个布局文件即控件都是如何摆放如何显示的,
// setContentView就是设置一个Activity的显示界面,这句话就是设置这个这句话所再的Activity采用R.layout下的main布局文件进行布局
//AudioManager类声明
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);//音量权限申请
// 权限支持
Permissions.check(this);
// 初始化界面
videoView = (VideoView)findViewById(R.id.id_videoview);// 播放器控件
videoBar = (SeekBar) findViewById(R.id.id_seekbar);//可拖放视频进度条
videoBar.setMax(100);//保险起见,注释掉也可以正茬运行
voiceBar = (SeekBar) findViewById(R.id.id_voiceBar);//音量进度条
lightBar = (SeekBar) findViewById(R.id.id_lightBar);//亮度进度条
//消息支持
msgHandler = new MyHandler();
//Handler是Android SDK来处理异步消息的核心类。
//子线程与主线程通过Handler来进行通信。子线程可以通过Handler来通知主线程进行UI更新。
//测试方法
initViews();//初始化
initLight();
initVoice();
// 播放控制(暂停、继承)
final ImageButton playstop = (ImageButton) findViewById(R.id.id_paly_pause);//播放键
playstop.setOnClickListener(new View.OnClickListener() {//监听器,点击按钮调用另外的方法
@Override
public void onClick(View v) {
if(videoView.isPlaying())
{
videoView.pause();
playstop.setImageDrawable(getDrawable(R.drawable.go_next1));
}
else
{
videoView.start();
playstop.setImageDrawable(getDrawable(R.drawable.pause3));
}
}
});
//播放控制(进度播放)
videoBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// * 拖动条进度改变的时候调用
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// * 拖动条开始拖动的时候调用
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {// * 拖动条停止拖动的时候调用
// 控制视频跳转到目标位置
if(videoView.isPlaying())
{
int progress = seekBar.getProgress();//获取当前seekBar的值
int position = progress * videoView.getDuration() / 100;//getDuration()获取载入的视频文件的时长
videoView.seekTo( position );//从指定位置播放视频
}
}
});
// 控制面板的显示和隐藏
controlBar = findViewById(R.id.id_control_bar);//三個佈局,下方
rotateBar = findViewById(R.id.id_rotate_bar);//侧
titleBar = findViewById(R.id.id_title_bar);//头部
videoView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, “点中画面…”);
if(controlBar.getVisibility() == View.GONE)
{
controlBar.setVisibility(View.VISIBLE);//显示面板
rotateBar.setVisibility(View.VISIBLE);
titleBar.setVisibility(View.VISIBLE);
}
else
{
controlBar.setVisibility(View.GONE);//隐藏面板
rotateBar.setVisibility(View.GONE);
titleBar.setVisibility(View.GONE);
Log.d(TAG, “隐藏播放控制面板…”);
}
return false;
}
});
// 接受外部调用
Intent intent = getIntent();
Uri mediaUri = intent.getData();
/**理解URI和URL的区别,我们引入URN这个概念。
URI = Universal Resource Identifier 统一资源标志符
URL = Universal Resource Locator 统一资源定位符
URN = Universal Resource Name 统一资源名称
这三者的关系如下图:
也就是说,URI分为三种,URL or URN or (URL and URI)
URL代表资源的路径地址,而URI代表资源的名称。
通过URL找到资源是对网络位置进行标识,:*/
if(mediaUri != null)
{
videoView.setVideoURI(mediaUri);
videoView.start();
}
}
@Override
protected void onStart()
{
//只是在activity界面启动时候调用,如何想要在播放后实行横竖屏切换就要重新创造一个activity
//或者刷新界面
//横屏播放控制
if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)//横向打印
{ // 取得当前屏幕方向,如果不是横屏打印,就换成横屏打印
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onStart();
if(timer == null)
{
// 启动定时器(间隔500ms)
timerTask = new MyTimerTask();
timer = new Timer();
timer.schedule(timerTask, 500, 500);//就是看进度条跑的速度
}
}
@Override
protected void onStop()
{
if(timer != null)
{
//本界面隐藏时,要停止定时器(因为本界面已经隐藏了,如果继续刷新界面将毫无意义、白白耗费CPU)
timer.cancel();
timer = null;
}
super.onStop();
}
//点击 '横竖屏切换'按钮
// 点击 '打开' 按钮
// public void openFile(View view)
// {
// intent = new Intent( Intent.ACTION_GET_CONTENT);
// //ACTION_GET_CONTENT:允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
// intent.setType(“video/"); // 只显示视频
// // setType()就是设置默认用哪种格式打开,比如"video/”,"audio/amr"是音频
// startActivityForResult(intent, REQ_OPEN_FILE);
// }
public void openFile(View view)
{
intent=new Intent(Intent.ACTION_GET_CONTENT);
//ACTION_GET_CONTENT:允许用户选择特殊格式的数据,并返回(特殊种类的数据,照一张照片或一段录音)
intent.setType(“video/");
//setType()就是设置默认用哪种格式打开,比如"video/”,“audio/amr”
startActivityForResult(intent,REQ_OPEN_FILE);//请求打开文件
}
//点击分享,这个功能有问题
public void share(String videopath)
{
Uri videoUri=Uri.fromFile(new File(videopath));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, videoUri);
shareIntent.setType(“audio/*”);
startActivity(Intent.createChooser(shareIntent, “分享到”));
}
// 从媒体管理器返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQ_OPEN_FILE)
{
if(resultCode == RESULT_OK)
{
Uri mediaUri = data.getData();
videoView.setVideoURI(mediaUri);
videoView.start();
//分享
Cursor cursor=getContentResolver().query(mediaUri,null,null,null,null);
cursor.moveToFirst();
String videopath=cursor.getString(1);
if(videopath!=null)
{
Message msg=Message.obtain();
msg.what=5;
msg.obj=videopath;
msgHandler.sendMessage(msg);
}
}
}
}
//更新显示进度:duration总时长 position 当前播放位置时长
public void showProgess(int duration,int position)
{
//转成百分比
int percent = position * 100/duration;
videoBar.setProgress(percent);
}
//定时器任务
private class MyTimerTask extends TimerTask
{
@Override
public void run()
{
//如果videoView没有在播放,就不做什么
if(!videoView.isPlaying())
return;
//取得当前播放进度
int duration=videoView.getDuration();//取得视频的总时长 单位 毫秒
int position=videoView.getCurrentPosition();//获取视频当前播放位置 单位 毫秒
Message msg = Message.obtain();
msg.what = 1;
msg.arg1 = duration;
msg.arg2 = position;
msgHandler.sendMessage(msg);
}
}
//消息支持
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
if(msg.what==1)
{
int duration=msg.arg1;
int position=msg.arg2;
showProgess(duration,position);
}
if(msg.what==5)
{
final String videopath=String.valueOf(msg.obj);
ImageButton btnshare=(ImageButton)findViewById(R.id.id_share);
btnshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
share(videopath);
}
});
}
}
}
//返回按钮
public void doReturn(View view)
{
Intent intent = new Intent(this,UserloginActivity.class);
startActivity(intent);
}
//获取屏幕亮度
private void initLight()
{
float currentBright = 0.0f;
try {
// 系统亮度值范围:0~255,应用窗口亮度范围:0.0f~1.0f。
currentBright = android.provider.Settings.System.getInt(
getContentResolver(),
//ContentResolver Context提供getContentResolver()方法可以获取ContentResolver对象,
//便可以操作前边所提的SettingsProvider所暴露的数据。
android.provider.Settings.System.SCREEN_BRIGHTNESS) * 100;
//固定写法,获取屏幕亮度
} catch (Exception e)
{
e.printStackTrace();
}
lightBar.setProgress((int) currentBright);
}
//获取系统音量
private void initVoice() {
// 获取系统最大音量
maxVoice = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//////////////////////////
//明天查找AudioManager的方法有哪些,获取系·统最大音量的方法有没有出错
// 设置voice_seekbar的最大值
voiceBar.setMax(maxVoice);
// 获取到当前 设备的音量
currentVoice = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}
//调节屏幕亮度和系统音量
private void initViews() {
this.voiceBar = (SeekBar) findViewById(R.id.id_voiceBar);
this.lightBar = (SeekBar) findViewById(R.id.id_lightBar);
this.voiceBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 设置音量
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
});
// 调节亮度
this.lightBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(progress) * (1f / 100f);
// 调节亮度
getWindow().setAttributes(lp);
}
});
}
}
Permissions类:
package example.video3;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
public class Permissions extends AppCompatActivity {
// 检查和申请权限
static final int PERMISSION_REQ_CODE = 1;
public static void check(Activity activity)
{
// 要申请的权限列表
final String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,//外部存儲
Manifest.permission.INTERNET,//内部存儲
Manifest.permission.CAMERA//攝像
};
// 检查本应用是否有了 WRITE_EXTERNAL_STORAGE 权限
if (ContextCompat.checkSelfPermission(activity, permissions[0])
!= PackageManager.PERMISSION_GRANTED)
{
// 系统将弹出一个对话框,询问用户是否授权
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQ_CODE);
}
}
// 权限申请的结果
// requestCode:请求码
// permissions: 申请的N个权限
// grantResults: 每个权限是否被授权
}
RegisterActivity类:
package example.video3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void doRegister(View view)
{
//取得用户输入
String username = ((EditText)findViewById(R.id.id_username)).getText().toString();
String password = ((EditText)findViewById(R.id.id_password)).getText().toString();
String verify = ((EditText)findViewById(R.id.id_verify)).getText().toString();
if(username.equals(""))
{
Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
return;
}
else
{
if(password.equals(""))
{
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
if(password.length()<4)
{
Toast.makeText(this, "密码必须大于4位数", Toast.LENGTH_SHORT).show();
return;
}
if(verify.equals(""))
{
Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
return;
}
}
if(!password.equals(verify))
{
Toast.makeText(this, "前后密码输入不一致", Toast.LENGTH_SHORT).show();
return;
}
// 保存用户信息
File file = new File(getExternalFilesDir(""), "users.txt");
example.video3.UserManager um = new example.video3.UserManager(file);
//UserManager um = new UserManager(file);
Log.e("测试","加载信息");
try{
um.load(); // 从users.txt中读取数据
Log.e("测试","进入try");
}catch(Exception e)
{
Log.e("测试","进入catch");
}
//检查用户名是否存在
if(um.find(username)!=null)
{
Log.e("测试","用户名相同");
Toast.makeText(this, "此用户名已存在", Toast.LENGTH_SHORT).show();
}
else
{
// 添加用户,保存到文件
um.add(new User(username, password));
try
{
um.save();
} catch (Exception e)
{
e.printStackTrace();
}
Log.e("测试","用户名不同");
Toast.makeText(this, "注册成功!", Toast.LENGTH_SHORT).show();
finish();
}
}
public void doReturn(View view) {
Intent intent = new Intent(this,UserloginActivity.class);
startActivity(intent);
}
}
User类:
package example.video3;
/**
- Created by Lenovo on 2019/5/30.
*/
public class User
{
public String username;
public String password;
public User()
{
}
public User(String username,String password)
{
this.username=username;
this.password=password;
}
}
UserloginActivity类:
package example.video3;
/**
- Created by Lenovo on 2019/5/30.
*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import static example.video3.Global.i;
public class UserloginActivity extends AppCompatActivity {
Handler msgHandler = new Handler();
private boolean logon_choose = false;
private boolean remember_choose = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
super.onCreate(savedInstanceState);
setContentView(R.layout.userlogin);
//从配置里加载
SharedPreferences sharedPref=getPreferences(Context.MODE_PRIVATE);
//Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
String username=sharedPref.getString("username","");
String password=sharedPref.getString("password","");
logon_choose=sharedPref.getBoolean("logon_choose",false);
remember_choose=sharedPref.getBoolean("remember_choose",false);
if(remember_choose)
{
if(username.length()>0&&password.length()>6)
{
//自动显示上次登录信息
((EditText)findViewById(R.id.id_log_username)).setText(username);
((EditText)findViewById(R.id.id_log_password)).setText(password);
//如果勾选自动登录 延时N毫秒后自动登录
if(logon_choose)
{
autoLogin();
}
}
}
else
{
((EditText)findViewById(R.id.id_log_username)).setText(username);
}
CheckBox automatic = (CheckBox)findViewById(R.id.id_automatic_logon);
automatic.setChecked(logon_choose);
automatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
logon_choose=!logon_choose;
remember_choose=true;
}
});
final CheckBox remember = (CheckBox)findViewById(R.id.id_remember_password);
remember.setChecked(remember_choose);
remember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
remember_choose=!remember_choose;
}
});
}
// 延时后自动登录
private void autoLogin()
{
msgHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
doLogin(null);
}
}, 10000); // 使用PostDelayed方法,延时10000毫秒后执行一个Runnable线程
}
//点击登录按钮时
public void doLogin(View view)
{
//取得用户输入
String username=((EditText)findViewById(R.id.id_log_username)).getText().toString();
String password=((EditText)findViewById(R.id.id_log_password)).getText().toString();
//加载文件
File file = new File(getExternalFilesDir(""), "users.txt");
example.video3.UserManager um = new example.video3.UserManager(file);
////hahaha,出错很多次.......
try
{
um.load();
}catch(Exception e){}
// 从用户列表里查找用户
User u = um.find(username);
if(u == null)
{
Toast.makeText(this, "无此用户!", Toast.LENGTH_SHORT).show();
return;
}
if(!u.password.equals(password))
{
Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
((EditText)findViewById(R.id.id_log_password)).setText("");
return;
}
// 保存当前用户信息,以便下一次开机启动时加载
//打开Preferences,名称为sharedPref
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
//让sharePref处于可编辑状态
SharedPreferences.Editor editor = sharedPref.edit();
//存入数据
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("logon_choose",logon_choose);
editor.putBoolean("remember_choose",remember_choose);
//Editor editor = new Editor();
//当中Editor是一个改动SharedPreferences数据的接口。
//editor.putString( url , json);
//向键为url中的Editor中放入值 json。
//相同函数getString(String key);刚好与上面的那个方法相反取出值为key里面的的值。
editor.commit();
// 登录成功, 把用户信息放在全局对象里, 以便在各个Activity里都可以访问当前用户信息
i.username=username;
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
//点击注册按钮
public void doRegister(View view)
{
Intent intent=new Intent(this,RegisterActivity.class);
startActivity(intent);
}
}
UserManager类:
package example.video3;
/**
- Created by Lenovo on 2019/5/30.
*/
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
/**
- Created by 111 on 2018/5/11.
*/
public class UserManager {
//创建一个list来存储用户信息
ArrayList userlist = new ArrayList<>();
//保存数据在这个文件里
File file;
public UserManager(File file) {
this.file = file;
}
//保存文件
public void save() throws Exception {
//每行存储一个用户的信息,以逗号分隔
FileOutputStream fstream = new FileOutputStream(file);
//创建一个向指定File对象表示的文件中写入数据的文件输出流
for (User u : userlist) {
String line = u.username + "," + u.password + "\n";
fstream.write(line.getBytes("UTF-8"));
//getBytes(“utf-8”)是使用UTF-8编码表进行转换,将一个字符串转化为一个字节数组
}
try {
fstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//文件加载
public void load()
{
Log.e("测试","加载load");
InputStreamReader m = null;
try {
m = new InputStreamReader(new FileInputStream(file), "UTF-8");
//先获取字节流,然后创建InputStreamReader将字节流转化成字符流,并指定其字符集为UTF-8;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(m);
//将字符流以缓存的形式一行一行输出
userlist.clear();//先清空链表
while (true)
{
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null)
break;
String[] cols = line.split(",");
//split()是分割函数,上面的意思是以‘,’为划分依据来分割字符串然后以字符串数组的形式输出
if (cols.length < 2)
continue;
User u = new User();
u.username = cols[0].trim();
u.password = cols[1].trim();//Trim():删除字符串中多余的空格,但会在英文字符串中保留一个作为词与词之间分隔的空格。
userlist.add(u);
}
try {
reader.close(); // reader关闭后,底层的inputstream会被自动关闭
} catch (IOException e) {
e.printStackTrace();
}
}
//注册一个用户
public void add(User u)
{
userlist.add(u);
}
//按名称查找
public User find(String username)
{
Log.e("测试","调用find");
for(User u : userlist)
{
if(u.username.equals(username))
{
return u;
}
int i=1;
i++;
Log.e("测试",""+i);
}
Log.e("测试","用户信息列表"+userlist);
Log.e("测试","find返回空");
return null;
}
}


制作所需的图片(排列依次往下):













layout布局:
第一个

Text:
<VideoView
android:id="@+id/id_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<android.support.constraint.ConstraintLayout
android:id="@+id/id_rotate_bar"
android:layout_width="wrap_content"
android:layout_height="257dp"
android:orientation="vertical"
android:weightSum="1">
<ImageButton
android:id="@+id/id_share"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_marginBottom="-4dp"
android:layout_marginLeft="0dp"
android:background="#0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@android:drawable/ic_menu_set_as"/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:id="@+id/id_control_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#0000"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="9dp"
android:background="#0000"
android:onClick="openFile"
app:srcCompat="@drawable/menu"/>
<ImageButton
android:id="@+id/id_paly_pause"
android:layout_width="50dp"
android:layout_height="54dp"
android:layout_marginLeft="3dp"
android:background="#0000"
app:srcCompat="@drawable/button1"/>
<SeekBar
android:id="@+id/id_seekbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
/>
</LinearLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/id_title_bar"
android:layout_width="match_parent"
android:layout_height="101dp"
android:orientation="horizontal"
android:weightSum="1">
<ImageButton
android:id="@+id/id_imreturn"
android:layout_width="50dp"
android:layout_height="59dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#0000"
android:onClick="doReturn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/return1"/>
<ImageView
android:id="@+id/ic_voice"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="-6dp"
android:layout_marginLeft="16dp"
android:layout_weight="0.07"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/id_imreturn"
app:srcCompat="@drawable/voice1"/>
<SeekBar
android:id="@+id/id_lightBar"
android:layout_width="520dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintLeft_toRightOf="@+id/ic_voice"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/ic_light"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintLeft_toRightOf="@+id/id_imreturn"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_light"/>
<SeekBar
android:id="@+id/id_voiceBar"
android:layout_width="520dp"
android:layout_height="40dp"
android:layout_marginBottom="-17dp"
android:layout_marginLeft="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/ic_light"/>
</android.support.constraint.ConstraintLayout>
第二个

Text:
<![CDATA[
android:weightSum=“1”>
]]>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="0.04"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_marginLeft="8dp"
android:background="#0000"
android:onClick="doReturn"
app:srcCompat="@android:drawable/ic_menu_revert"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_weight="0.04"
android:layout_marginTop="30dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="4dp"
android:layout_weight="0.04"
app:srcCompat="@drawable/icon3"/>
<EditText
android:id="@+id/id_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="0.04"
android:ems="10"
android:hint="用户名"
android:inputType="textPersonName"/>
<EditText
android:id="@+id/id_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="0.04"
android:ems="10"
android:hint="输入密码"
android:inputType="textPassword"/>
<EditText
android:id="@+id/id_verify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="0.04"
android:ems="10"
android:hint="确认密码"
android:inputType="textPassword"/>
<Button
android:id="@+id/id__btnok"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_weight="0.04"
android:background="#7BBCFC"
android:onClick="doRegister"
android:text="注册"
android:textColor="#ffff"
android:textSize="24sp"/>
</LinearLayout>
第三个:

Text:
<?xml version="1.0" encoding="utf-8"?><FrameLayout
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:orientation="vertical"
android:background="@drawable/playground6"
tools:context="example.videoplay.UserloginActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginTop="60dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="0.01"
android:background="#0000"
app:srcCompat="@drawable/icon4"/>
<EditText
android:id="@+id/id_log_username"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="4dp"
android:layout_marginTop="10dp"
android:layout_weight="0.01"
android:ems="10"
android:hint="用户名"
android:inputType="textPersonName"
android:padding="10dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"/>
<EditText
android:id="@+id/id_log_password"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="4dp"
android:layout_marginTop="10dp"
android:layout_weight="0.01"
android:ems="10"
android:hint="密码"
android:inputType="textPassword"
android:padding="10dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"/>
<Button
android:id="@+id/id_login"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_margin="10dp"
android:layout_marginTop="20dp"
android:layout_weight="0.01"
android:background="#97D9E5"
android:onClick="doLogin"
android:paddingBottom="8dp"
android:paddingTop="10dp"
android:text="登录"
android:textSize="24sp"/>
<LinearLayout
android:id="@+id/id_choose_linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_weight="0.01"
android:orientation="horizontal">
<CheckBox
android:id="@+id/id_automatic_logon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="35dp"
android:layout_weight="1"
android:text="自动登录"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/id_remember_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:text="记住密码"
android:textSize="20sp"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_marginTop="10dp"
android:layout_weight="0.01"
android:background="#0000"
android:onClick="doRegister"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="点击注册"
android:textColor="#7BBCFC"
android:textSize="20sp"/>
</LinearLayout>
接下来:

这个是我用的图标:

本文介绍了使用Android开发的一款视频播放器应用,包括基本的视频播放功能和视频管理。用户可以登录并播放手机本地视频,支持进度拖动、亮度和音量调节。项目涉及Swing图形界面、数据库交互和Android多媒体播放技术。
789

被折叠的 条评论
为什么被折叠?



