资源准备:把视频资源放到sd卡中
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210602163004939.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hKQ1hKQ3hpYw==,size_16,color_FFFFFF,t_70)
视图代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity3Video">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_vv" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="查找"
android:id="@+id/btn_vv" />
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vv" />
</LinearLayout>
java代码:
package com.example.androidmedia26;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity3Video extends AppCompatActivity {
private VideoView vv;
private MediaController controller;
private EditText ed_vv;
private Button btn_vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3_video);
vv = findViewById(R.id.vv);
ed_vv = findViewById(R.id.ed_vv);
btn_vv = findViewById(R.id.btn_vv);
controller = new MediaController(this);
btn_vv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
Uri uri=data.getData();
ed_vv.setText(uri.toString());
vv.setVideoURI(uri);
vv.setMediaController(controller);
controller.setMediaPlayer(vv);
vv.requestFocus();vv.start();
}
}
}
}
效果:
一、点击查找:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210603162534631.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hKQ1hKQ3hpYw==,size_16,color_FFFFFF,t_70)
二、选择视频:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210603162557324.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hKQ1hKQ3hpYw==,size_16,color_FFFFFF,t_70)
三、自动播放:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210603162614822.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hKQ1hKQ3hpYw==,size_16,color_FFFFFF,t_70)