发布于 2017-09-13 02:18:29 | 200 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


本篇文章主要介绍了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"/>
...

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

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务