package com.example.yuekang.your_soul;
import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.LinearLayout;
import android.app.Activity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.MotionEventCompat;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.Inet6Address;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private VideoView videoView,videoView1,videoView2;
private LinearLayout threevideoview,shurujiemian;
private int pingmuweizhi=0;
private Button qr;
private EditText et;
private TextView tv,sptv;
private float s0,s1,middle0,middle1,m0,m1,e0,e1, xxiangchajueduizhi,yxiangchajueduizhi;
private int pdbz=0,xzhengfu=1,yzhengfu=1;
private float pingmukuandu,pingmuchangdu;
private DisplayMetrics pingmu;
private Socket socket;
private String baocunlujing;
private ArrayList<String> arrayListst=new ArrayList<String>();
private String baocunjiewei;
public boolean onTouchEvent(MotionEvent event){
int action=MotionEventCompat.getActionMasked(event);
switch (action){
case MotionEvent.ACTION_DOWN:
s0=event.getX();
s1=event.getY();
middle0=s0;
middle1=s1;
break;
case MotionEvent.ACTION_MOVE:
m0=event.getX();
m1=event.getY();
xxiangchajueduizhi=m0-middle0;
yxiangchajueduizhi=m1-middle1;
xzhengfu=1;
yzhengfu=1;
if (xxiangchajueduizhi<0){
xxiangchajueduizhi=-xxiangchajueduizhi;
xzhengfu=-1;
}
if(yxiangchajueduizhi<0){
yxiangchajueduizhi=-yxiangchajueduizhi;
yzhengfu=-1;
}
tv.setText(String.valueOf(yxiangchajueduizhi));
if (pdbz==0 && xxiangchajueduizhi>yxiangchajueduizhi){
threevideoview.setX(threevideoview.getX()+xxiangchajueduizhi*xzhengfu);
pdbz=1;
}
else if(pdbz==0 && yxiangchajueduizhi>xxiangchajueduizhi){
threevideoview.setY(threevideoview.getY()+yxiangchajueduizhi*yzhengfu);
pdbz=2;
}
else if(pdbz==1){
threevideoview.setX(threevideoview.getX()+xxiangchajueduizhi*xzhengfu);
}
else if(pdbz==2){
threevideoview.setY(threevideoview.getY()+yxiangchajueduizhi*yzhengfu);
}
middle0=m0;
middle1=m1;
sptv.setY(100-3*pingmuchangdu);
break;
case MotionEvent.ACTION_UP:
e0=event.getX();
e1=event.getY();
if (pdbz==1){
if (e0-s0>200){
threevideoview.animate().translationX(pingmukuandu).setDuration(100).setStartDelay(0);
}
else if(e0-s0<-200){
threevideoview.animate().translationX(-pingmukuandu).setDuration(100).setStartDelay(0);
}
else {
threevideoview.animate().translationX(0).setDuration(100).setStartDelay(0);
}
pdbz=0;
}
else if(pdbz==2) {
if (e1 - s1 > 200) {
if (pingmuweizhi == 0) {
threevideoview.animate().translationY(pingmuchangdu).setDuration(100).setStartDelay(0);
}
else if (pingmuweizhi == 2) {
videoView1.pause();
threevideoview.animate().translationY(pingmuchangdu).setDuration(100).setStartDelay(0);
videoView.start();
pingmuweizhi = 0;
}
else if (pingmuweizhi == 4) {
videoView2.pause();
threevideoview.animate().translationY(0).setDuration(100).setStartDelay(0);
videoView1.start();
pingmuweizhi = 2;
}
} else if (e1 - s1 < -200) {
if (pingmuweizhi == 0) {
videoView.pause();
threevideoview.animate().translationY(0).setDuration(100).setStartDelay(0);
videoView1.start();
pingmuweizhi = 2;
}
else if (pingmuweizhi == 2) {
videoView1.pause();
threevideoview.animate().translationY(-pingmuchangdu).setDuration(100).setStartDelay(0);
videoView2.start();
pingmuweizhi = 4;
}
else if (pingmuweizhi == 4) {
threevideoview.animate().translationY(-pingmuchangdu).setDuration(100).setStartDelay(0);
}
} else {
if (pingmuweizhi == 0) {
threevideoview.animate().translationY(pingmuchangdu).setDuration(100).setStartDelay(0);
}
else if (pingmuweizhi == 2) {
threevideoview.animate().translationY(0).setDuration(100).setStartDelay(0);
}
else if (pingmuweizhi == 4) {
threevideoview.animate().translationY(-pingmuchangdu).setDuration(100).setStartDelay(0);
}
threevideoview.animate().translationY(0).setDuration(100).setStartDelay(0);
}
pdbz = 0;
break;
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baocunjiewei="";
baocunlujing= String.valueOf(this.getExternalFilesDir(null));
new Thread(new socketthread(socket,baocunlujing)).start();
threevideoview=findViewById(R.id.threevideoView);
shurujiemian=findViewById(R.id.srjm);
///屏幕长宽
pingmu=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(pingmu);
pingmukuandu=pingmu.widthPixels;
pingmuchangdu=pingmu.heightPixels;
LinearLayout.LayoutParams shipinchangkuanbuju=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
shipinchangkuanbuju.height= (int) (pingmuchangdu*3);
shipinchangkuanbuju.width= (int) pingmukuandu;
threevideoview.setLayoutParams(shipinchangkuanbuju);
///申请权限
int myPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (myPermission != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "请求读取SD卡权限", Toast.LENGTH_SHORT).show();
// 动态申请权限,请求码为1
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else {
}
et=findViewById(R.id.sr);
qr=findViewById(R.id.qd);
tv=findViewById(R.id.jg);
sptv=findViewById(R.id.sptv);
sptv.setY(-pingmuchangdu*3+100);
// 获取VideoView实例
videoView = findViewById(R.id.videoView);
videoView1=findViewById(R.id.videoView1);
videoView2=findViewById(R.id.videoView2);
threevideoview.setY(pingmuchangdu-100);
shurujiemian.setY(pingmuchangdu+100);
// 设置视频路径
String videoPath=this.getExternalFilesDir(null)+"/v1.mp4";
Uri uri = Uri.parse(videoPath);
///videoView.setVideoURI(uri);
videoView.setVideoPath(videoPath);
// 创建媒体控制器
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
// 开始播放视频
videoView.start();
String videoPath1 = this.getExternalFilesDir(null)+"/v2.mp4";
Uri uri1 = Uri.parse(videoPath1);
videoView1.setVideoURI(uri1);
// 创建媒体控制器
MediaController mediaController1 = new MediaController(this);
mediaController1.setAnchorView(videoView1);
videoView1.setMediaController(mediaController1);
// 开始播放视频
videoView1.start();
videoView1.pause();
String videoPath2 = this.getExternalFilesDir(null)+"/v3.mp4";
Uri uri2 = Uri.parse(videoPath2);
videoView2.setVideoURI(uri2);
// 创建媒体控制器
MediaController mediaController2 = new MediaController(this);
mediaController2.setAnchorView(videoView2);
videoView2.setMediaController(mediaController2);
// 开始播放视频
videoView2.start();
videoView2.pause();
// 判断手机中是否装载了sd卡
boolean isLoadSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(isLoadSDCard){
///File[] evi = Environment.getExternalStorageDirectory().listFiles();
tv.setText(Environment.getExternalStorageDirectory().toString());
///for (int i = 0; i<evi.length; i++){
/// tv.setText(tv.getText()+"\n"+evi[i].toString());
///}
///inflatelv(Environment.getExternalStorageDirectory().listFiles());
}
else {
tv.setText("不能读取");
}
et.setText(this.getExternalFilesDir(null)+"/v1.mp4");
qr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String xx=et.getText().toString();
Uri uri = Uri.parse(xx);
videoView.setVideoURI(uri);
videoView.start();
}
});
}
class socketthread implements Runnable{
private Socket socket;
private String baocunlujing;
private String chushixierushuju="";
public socketthread(Socket socket,String baocunlujing) {
this.socket=socket;
this.baocunlujing=baocunlujing;
}
@Override
public void run() {
String[] xx0="hj0".split("0");
try {
socket=new Socket(Inet6Address.getByName("你的ipv6地址"),端口号);
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream= null;
OutputStream outputStream=null;
OutputStream wenjianweizhi=null;
try {
inputStream = socket.getInputStream();
outputStream=socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes=new byte[1024];
int byteschangdu;
while (true){
System.out.println(chushixierushuju+"0000000");
if (chushixierushuju!=""){
System.out.println(chushixierushuju+"1111111");
if (chushixierushuju.contains("start_receive_file")) {
String[] wenjianming = chushixierushuju.split("start_receive_file")[1].split("f e n g e f u");
try {
wenjianweizhi = new FileOutputStream(baocunlujing + "/" + wenjianming[0]);
List<String> listzhongjj = new ArrayList<String>(Arrays.asList(wenjianming));
String chushixierushuju1="";
if (wenjianming.length!=1){
chushixierushuju1 = String.join(listzhongjj.remove(0));
}
chushixierushuju=copyFile(inputStream,wenjianweizhi,chushixierushuju1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else try {
assert inputStream != null;
byteschangdu = inputStream.read(bytes);
String nianbaoshuju=new String(bytes,0,byteschangdu);
if (nianbaoshuju.contains("start_receive_file")) {
String[] wenjianming = nianbaoshuju.split("start_receive_file")[1].split("f e n g e f u");
wenjianweizhi = new FileOutputStream(baocunlujing + "/" + wenjianming[0]);
List<String> listzhongjj = new ArrayList<String>(Arrays.asList(wenjianming));
chushixierushuju = String.join(listzhongjj.remove(0));
chushixierushuju = copyFile(inputStream,wenjianweizhi,chushixierushuju);
System.out.println(chushixierushuju);
}
///jieshouxiangying(inputStream,nianbaoshuju);
bytes=new byte[1024];
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void jieshouxiangying(InputStream inputStream,String nianbaoshuju){
try {
if (nianbaoshuju.contains("start_receive_file")) {
String[] wenjianming = nianbaoshuju.split("start_receive_file")[1].split("f e n g e f u");
OutputStream wenjianweizhi = new FileOutputStream(baocunlujing + "/" + wenjianming[0]);
List<String> listzhongjj = new ArrayList<String>(Arrays.asList(wenjianming));
String chushixierushuju = String.join(listzhongjj.remove(0));
/
copyFile(inputStream,wenjianweizhi,chushixierushuju);
}
}catch(IOException e){
e.printStackTrace();
}
}
private String copyFile(InputStream in, OutputStream out, String chushixieruxinxi) throws IOException {
byte[] buffer = new byte[1024];
int read;
read = in.read(buffer);
if(chushixieruxinxi!=""){
buffer=chushixieruxinxi.getBytes();
out.write(buffer,0,buffer.length);
}
while (true){
if (new String(buffer,0,read).contains("end")) {
System.out.println(new String(buffer,0,read));
String[] zhongjianti=new String(buffer,0,read).split("end");
buffer=zhongjianti[0].getBytes();
if(zhongjianti.length==2){
chushixieruxinxi=zhongjianti[1];
}
else {
chushixieruxinxi="";
}
out.write(buffer,0,buffer.length);
out.flush();
out.close();
return chushixieruxinxi;
}
else {
out.write(buffer, 0, read);
buffer = new byte[1024];
read = in.read(buffer);
}
}
}
}
layout
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/srjm"
android:gravity="center_horizontal"
android:orientation="horizontal">
<EditText
android:id="@+id/sr"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="/storage/emulated/0/video.mp4"/>
<Button
android:id="@+id/qd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/threevideoView"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<VideoView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/videoView"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<VideoView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/videoView1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<VideoView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/videoView2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/sptv"
android:text="视频"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/jg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="123456789"/>
</LinearLayout>
</LinearLayout>