前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android视频播放简单实现示例(VideoView&MediaPlayer)

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

作者头像
砸漏
发布2020-10-22 10:09:07
1.4K0
发布2020-10-22 10:09:07
举报
文章被收录于专栏:恩蓝脚本

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

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

代码语言:javascript
复制
<?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 作为视频播放时的显示位置。

代码语言:javascript
复制
 private VideoView videoView;

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

代码语言:javascript
复制
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() 来实现视频播放初始化

代码语言:javascript
复制
 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 中对权限的取得结果进行判断,并针对性操作。如果获得了权限,就执行初始化;如果没有获得权限,就提示用户。

代码语言:javascript
复制
 @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(重新播放)的逻辑。

代码语言:javascript
复制
 @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;
  }
 }

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

代码语言:javascript
复制
 @Override
 protected void onDestroy() {
  super.onDestroy();
  if(videoView != null){
   videoView.suspend();
  }
 }

完整代码示例:

代码语言:javascript
复制
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 中配置相应的权限。

代码语言:javascript
复制
<?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"/ 
...

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云点播
面向音视频、图片等媒体,提供制作上传、存储、转码、媒体处理、媒体 AI、加速分发播放、版权保护等一体化的高品质媒体服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档