播放音频和视频的逻辑是一样的,下面我就单一的对播放视频的方法进行简单的论述。我也一个刚刚入门的Android的开发者,我将一步步的把我的历程写在我的博客上。
视频的播放和音频的播放复杂度相差不大,主要是通过VideoView类来进行实现的。
其中有几个常用的方法:
- setVideoPath()设置播放的视频文件的位置;
- 开始()开始或继续播放视频;
- 暂停()暂停播放视频;
- 恢复()将视频重新开始播放;
- IsPlaying模块()判断当前是否正在播放视频;
- getDuration()获取载入的视频文件的时长;
- seekTo()从指定的位置开始播放视频。
下面我们就进入实例的体验:
1.在新建的项目的XML中设置我们的布局:
三个按钮分别是:播放,暂停,重新播放,其次是一个Video View,视频就在这里播放。
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.codoon.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Play"/>
<Button
android:id="@+id/pause"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Pause"/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Stop"/>
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
2.我直接上代码
代码和音频的播放几乎一样,只是将音频播放器,转换成了视频播放器。
package com.example.codoon.myapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private 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 stop = findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
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();
}
}
//指定文件的路径,此处如果找不到具体的路径,可以下载一个ES文件浏览器
private void initVideoPath(){
String file_path = "/storage/emulated/0/DCIM/Camera/VID_20180610_212931.mp4";
videoView.setVideoPath(file_path);
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
initVideoPath();
}else {
Toast.makeText(this,"拒绝权限,您将无法使用!",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.play:
if (!videoView.isPlaying()){
videoView.start(); //开始播放
}
break;
case R.id.pause:
if(videoView.isPlaying()){
videoView.pause(); //暂停播放
}
break;
case R.id.stop:
if (videoView.isPlaying()){
videoView.resume(); //重新播放
}
break;
}
}
//释放并清理播放器
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null){
videoView.suspend();
}
}
}