首先,我们需要在手机的SD卡目录下存放一个视频,手机默认的根目录是/storage/emulated/0,我们可以按照下面代码里的方法,得到这个根目录,也可以自己写,
我们在布局文件当中只布置三个按钮,分别用来开始播放(play),暂停(pause),结束(replay)这里不做详细的说明。
VideoView的主要方法
setVideoPath() 设置播放视频的文件位置
start() 开始或继续播放视频
pause() 暂停播放
resume() 将视频从头开始
seekTo() 从指定位置播放
isPlaying() 判断当前是否在播放
getDuration() 获取载入视频文件的时长
****在AndroidManifest.xml文件增加如下的两句权限获取。****
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.dede.mypplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
.........
</application>
看一看xml文件,加入VedioView控件
<?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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
tools:layout_editor_absoluteX="5dp"
tools:layout_editor_absoluteY="25dp" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="pause"
tools:layout_editor_absoluteX="118dp"
tools:layout_editor_absoluteY="24dp" />
<Button
android:id="@+id/replay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="replay"
tools:layout_editor_absoluteX="253dp"
tools:layout_editor_absoluteY="25dp" />
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView=findViewById(R.id.video_view);
Button play = findViewById(R.id.play);
Button pause = findViewById(R.id.pause);
Button replay = findViewById(R.id.replay);
play.setOnClickListener(this);
pause.setOnClickListener(this);
replay.setOnClickListener(this);
//为app动态获取访问SD卡的权限
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
else
{
initVideoPath();
}
}
//初始化MediaPlayer
private void initVideoPath() {
File file=new File(Environment.getExternalStorageDirectory(),"test.mp4");
if(file.exists())
videoView.setVideoPath(file.getPath());
else Toast.makeText(this,"视频不存在",Toast.LENGTH_SHORT).show();
}
//权限获取函数的回调函数
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 1:
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED)
{initVideoPath();}
else{
finish();
Toast.makeText(this,"denied!",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
if(!videoView.isPlaying())
{videoView.start();
Toast.makeText(this,"开始播放",Toast.LENGTH_SHORT).show();}
break;
case R.id.pause:
if(videoView.isPlaying())
videoView.pause();
break;
case R.id.replay:
if(videoView.isPlaying())
videoView.resume();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView!=null)
{
videoView.suspend();
}
}
}