android视频播放简单实现示例(VideoView&MediaPlayer)

时间:2022-07-27
本文章向大家介绍android视频播放简单实现示例(VideoView&MediaPlayer),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

如果你看过我的《android音乐播放简单实现(MediaPlayer)》,那么本篇将会毫无压力。

首先是主界面的三个按钮和一个播放控件

<?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:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.cofox.myplayvideo.MainActivity" 

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" 

  <Button
   android:id="@+id/btnPlay"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Play"
   android:textAllCaps="false" / 

  <Button
   android:id="@+id/btnPause"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Pause"
   android:textAllCaps="false" / 

  <Button
   android:id="@+id/btnReplay"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="Replay"
   android:textAllCaps="false" / 
 </LinearLayout 
 <VideoView
  android:id="@+id/vdvwFilm"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" / 

</LinearLayout 

在 MainActivity.java 中这里需要用到的是 VideoView 作为视频播放时的显示位置。

 private VideoView videoView;

在 onCreate 里,对界面的按钮和显示位置实例化,并检查权限。

videoView = (VideoView)findViewById(R.id.vdvwFilm);
  Button btnPlay = (Button)findViewById(R.id.btnPlay);
  Button btnPause = (Button)findViewById(R.id.btnPause);
  Button btnReplay = (Button)findViewById(R.id.btnReplay);

  btnPlay.setOnClickListener(this);
  btnPause.setOnClickListener(this);
  btnReplay.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
  }

用一个单独的方法 initVideoPath() 来实现视频播放初始化

 private void initVideoPath() {
  File file = new File(Environment.getExternalStorageDirectory(), "movie2.mp4");
  videoView.setVideoPath(file.getPath());//指定视频文件路径
  videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
   @Override
   public void onPrepared(MediaPlayer mp) {
    mp.setLooping(true);//让电影循环播放
   }
  });
 }

onRequestPermissionsResult 中对权限的取得结果进行判断,并针对性操作。如果获得了权限,就执行初始化;如果没有获得权限,就提示用户。

 @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_LONG).show();
     finish();
    }
    break;
   default:
    break;
  }
 }

在一个 onClick 方法中,统一处理 Play(播放)、Pause(暂停)、Replay(重新播放)的逻辑。

 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.btnPlay:
    if(!videoView.isPlaying()){
     videoView.start();//播放
    }
    break;
   case R.id.btnPause:
    if(videoView.isPlaying()){
     videoView.pause();//暂停
    }
    break;
   case R.id.btnReplay:
    if(videoView.isPlaying()){
     videoView.resume();//重新播放
    }
    break;
  }
 }

执行完毕,释放所有资源。

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if(videoView != null){
   videoView.suspend();
  }
 }

完整代码示例:

package com.cofox.myplayvideo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.os.EnvironmentCompat;
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);
videoView = (VideoView)findViewById(R.id.vdvwFilm);
Button btnPlay = (Button)findViewById(R.id.btnPlay);
Button btnPause = (Button)findViewById(R.id.btnPause);
Button btnReplay = (Button)findViewById(R.id.btnReplay);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnReplay.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(), "movie2.mp4");
videoView.setVideoPath(file.getPath());//指定视频文件路径
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);//让电影循环播放
}
});
}
@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_LONG).show();
finish();
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnPlay:
if(!videoView.isPlaying()){
videoView.start();//播放
}
break;
case R.id.btnPause:
if(videoView.isPlaying()){
videoView.pause();//暂停
}
break;
case R.id.btnReplay:
if(videoView.isPlaying()){
videoView.resume();//重新播放
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
videoView.suspend();
}
}
}

在 AndroidManifest.xml 中配置相应的权限。

<?xml version="1.0" encoding="utf-8"? 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cofox.myplayvideo" 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ 
...

然后我们就可以看到运行结果了。

以上就是本文的全部内容,希望对大家的学习有所帮助。