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

 更新时间:2017年08月25日 08:59:20   作者:厚土火焰山  
本篇文章主要介绍了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"/>
...

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

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

相关文章

  • Android中利用zxing实现自己的二维码扫描识别详解

    Android中利用zxing实现自己的二维码扫描识别详解

    这篇文章主要给大家介绍了关于Android中利用zxing实现自己的二维码扫描识别的相关资料,文中通过图文介绍的非常详细,对大家学习或者使用zxing具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-09-09
  • Android实现精确到天时分秒的抢购倒计时

    Android实现精确到天时分秒的抢购倒计时

    这篇文章主要为大家详细介绍了Android实现精确到天时分秒的抢购倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Android 画中画模式的实现示例

    Android 画中画模式的实现示例

    这篇文章主要介绍了Android 画中画模式的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Android编程设置屏幕亮度的方法

    Android编程设置屏幕亮度的方法

    这篇文章主要介绍了Android编程设置屏幕亮度的方法,结合实例形式分析了Android获取及设置屏幕亮度的相关实现技巧,需要的朋友可以参考下
    2017-07-07
  • Android自定义view实现水波进度条控件

    Android自定义view实现水波进度条控件

    这篇文章主要为大家详细介绍了Android自定义view实现水波进度条控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android高级组件AutoCompleteTextView自动完成文本框使用详解

    Android高级组件AutoCompleteTextView自动完成文本框使用详解

    这篇文章主要介绍了Android高级组件AutoCompleteTextView自动完成文本框的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android实现横向二级菜单

    Android实现横向二级菜单

    这篇文章主要为大家详细介绍了Android实现横向二级菜单的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Android startActivityForResult的基本用法详解

    Android startActivityForResult的基本用法详解

    这篇文章主要介绍了Android startActivityForResult的基本用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Activity/Fragment结束时处理异步回调的解决方案

    Activity/Fragment结束时处理异步回调的解决方案

    这篇文章主要介绍了关于在Activity/Fragment结束时处理异步回调的解决方案,文中介绍的非常详细,对大家的学习或者工作具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • Android无需root实现apk的静默安装

    Android无需root实现apk的静默安装

    这篇文章主要介绍了Android无需root实现apk的静默安装 的相关资料,需要的朋友可以参考下
    2016-01-01

最新评论