视屏播放主要使用VideoView来实现。他将视屏的显示和控制集一身,使用它可以做一个简单的视频播放器。
File file = new File(Environment.getExternalStorageDirectory(), path);
videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
videoView.start(); // 开始播放
videoView.pause(); // 暂停播放
videoView.resume(); // 重新播放
完整代码:
//视频播放
public class MainActivity extends AppCompatActivity {
private VideoView videoView;
private Button play;
private Button pause;
private Button replay;
private MediaController mediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
videoView = (VideoView) findViewById(R.id.video_view);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
replay = (Button) findViewById(R.id.replay);
// //初始化videoview控制条
// mediaController=new MediaController(this);
// //设置videoview的控制条
// videoView.setMediaController(mediaController);
// //设置显示控制条
// mediaController.show(0);
}
private void initData() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
initVideoPath("123.mp4"); // 初始化MediaPlayer
}
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!videoView.isPlaying()) {
videoView.start(); // 开始播放
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause(); // 暂停播放
}
}
});
replay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.resume(); // 重新播放
}
}
});
}
private void initVideoPath(String path) {
File file = new File(Environment.getExternalStorageDirectory(), path);
videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideoPath("movie.mp4");
} else {
Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();
}
}
}
对应xml:
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停" />
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播" />
</LinearLayout>
</RelativeLayout>
转发表明出处:https://blog.csdn.net/qq_35698774/article/details/107053363
点击下载源码
android互助群:
感谢:郭霖的《第一行代码 第二版》