Play video:
Playing video files is actually not more complicated than playing audio files. It is mainly realized by using the VideoView class. This class integrates the display and control of the video, so that we can complete a simple video player with only the help of it.
The usage of VideoView is similar to that of MediaPlayer, with the following common methods:
setVideoPath() sets the location of the video file to be played
start() start or continue playing the video
pause() Pause the video
resume() restart the video
seekTo() starts playing the video from the specified position
isPlaying() determines whether the video is currently playing
getDuration() Get the duration of the loaded video file
Create a new PlayVideoTest project
Modify the code in activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
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="Play"
/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Replay"
/>
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
In this layout file, three buttons are first placed to control the playback, pause, and replay of the video, and then another VideoView is placed under the button, and the later video will be displayed here:
Next, modify the code in MainActivity as follows:
package net.nyist.lenovo.playvideotest;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
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;
import java.io.File;
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);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button replay = (Button) findViewById(R.id.replay);
play.setOnClickListener(this);
pause.setOnClickListener(this);
replay.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();//初始化MediaPlayer
}
}
private void initVideoPath(){
File file = new File(Environment.getExternalStorageDirectory(),"movie.mp4");
videoView.setVideoPath(file.getPath());
}
@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
protected void onDestroy() {
super.onDestroy();
if (videoView!=null){
videoView.suspend();//将VideoView所占用的资源释放掉。
}
}
@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.replay:
if (!videoView.isPlaying()){
videoView.resume();//重新播放
break;
}
}
}
}
This part of the code is easy to understand, because it is very similar to the previous code for playing audio. First, a runtime permission processing is also performed in the onCreate() method, because the video file will be placed on the SD card.
When the user agrees to the authorization, the initVideoPath() method will be called to set the path of the video file. Here we need to place a video file named movie.mp4 in the root directory of the SD card in advance.
Let's take a look at the code in the click event of each button:
当点击Play按钮时会进行判断,
如果当前并没有正在播放视频,
则调用start()方法开始播放,
当点击Pause按钮时会判断,
如果当前视频正在播放,
则调用pause()方法暂停播放。
当点击Replay按钮时会判断,
如果当前视频正在播放,
则调用resume()方法从头播放视频。
Finally, in the onDestroy() method, we also need to call the suspend() method to release the resources occupied by VideoView.
In addition, always remember to declare the permissions used in the AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Click the Pause button to pause the video playback, and click the Replay button to restart the video.