欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android原生视频播放VideoView的使用

程序员文章站 2022-06-20 13:16:55
本文实例为大家分享了android原生视频播放videoview的具体代码,供大家参考,具体内容如下布局文件activity_video.xml

本文实例为大家分享了android原生视频播放videoview的具体代码,供大家参考,具体内容如下

布局文件activity_video.xml

<relativelayout 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"
 tools:context=".mainactivity">
 
 <videoview
  android:id="@+id/videoview"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
 <progressbar
  android:id="@+id/progressbar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerinparent="true" />
</relativelayout>

对应的avtivity:videoactivity.java

package com.example.administrator.main;
 
import android.content.intent;
import android.media.mediaplayer;
import android.net.uri;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.displaymetrics;
import android.view.motionevent;
import android.view.view;
import android.widget.mediacontroller;
import android.widget.progressbar;
import android.widget.relativelayout;
import android.widget.videoview;
 
public class videoactivity extends appcompatactivity {
 private progressbar progressbar;
 private videoview videoview;
 private mediacontroller mediacontroller;
 private int intpositionwhenpause = -1;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_video);
 
  //调用系统自带视频播放或者安装的第三方播放器
//  uri uri=uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
//  intent intent=new intent(intent.action_view);
//  intent.setdataandtype(uri,"video/*");
//  startactivity(intent);
 
  initvideoview();
 }
 
 /**
  * 初始化videoview播放
  */
 public void initvideoview() {
  //初始化进度条
  progressbar = (progressbar) findviewbyid(r.id.progressbar);
  //初始化videoview
  videoview = (videoview) findviewbyid(r.id.videoview);
  //初始化videoview控制条
  mediacontroller = new mediacontroller(this);
  //设置videoview的控制条
  videoview.setmediacontroller(mediacontroller);
  //设置显示控制条
  mediacontroller.show(0);
  //设置播放完成以后监听
  videoview.setoncompletionlistener(new mediaplayer.oncompletionlistener() {
   @override
   public void oncompletion(mediaplayer mp) {
 
   }
  });
  //设置发生错误监听,如果不设置videoview会向用户提示发生错误
  videoview.setonerrorlistener(new mediaplayer.onerrorlistener() {
   @override
   public boolean onerror(mediaplayer mp, int what, int extra) {
    return false;
   }
  });
  //设置在视频文件在加载完毕以后的回调函数
  videoview.setonpreparedlistener(new mediaplayer.onpreparedlistener() {
   @override
   public void onprepared(mediaplayer mp) {
    progressbar.setvisibility(view.gone);
    videoview.start();
   }
  });
  //设置videoview的点击监听
  videoview.setontouchlistener(new view.ontouchlistener() {
   @override
   public boolean ontouch(view v, motionevent event) {
    return false;
   }
  });
  //设置网络视频路径
  uri uri = uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
  videoview.setvideouri(uri);
  //设置为全屏模式播放
  setvideoviewlayoutparams(2);
 }
 
 /**
  * 设置videiview的全屏和窗口模式
  *
  * @param paramstype 标识 1为全屏模式 2为窗口模式
  */
 public void setvideoviewlayoutparams(int paramstype) {
  //全屏模式
  if (1 == paramstype) {
   //设置充满整个父布局
   relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent);
   //设置相对于父布局四边对齐
   layoutparams.addrule(relativelayout.align_parent_bottom);
   layoutparams.addrule(relativelayout.align_parent_top);
   layoutparams.addrule(relativelayout.align_parent_left);
   layoutparams.addrule(relativelayout.align_parent_right);
   //为videoview添加属性
   videoview.setlayoutparams(layoutparams);
  } else {
   //窗口模式
   //获取整个屏幕的宽高
   displaymetrics displaymetrics = new displaymetrics();
   this.getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics);
   //设置窗口模式距离边框50
   int videoheight = displaymetrics.heightpixels;
   int videowidth = displaymetrics.widthpixels;
   relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(videowidth, videoheight);
   //设置居中
   layoutparams.addrule(relativelayout.align_top);
   //为videoview添加属性
   videoview.setlayoutparams(layoutparams);
  }
 }
 
 /**
  * 页面暂停效果处理
  */
 @override
 protected void onpause() {
  super.onpause();
  //如果当前页面暂停则保存当前播放位置,全局变量保存
  intpositionwhenpause = videoview.getcurrentposition();
  //停止回放视频文件
  videoview.stopplayback();
 }
 
 /**
  * 页面从暂停中恢复
  */
 @override
 protected void onresume() {
  super.onresume();
  //跳转到暂停时保存的位置
  if (intpositionwhenpause >= 0) {
   videoview.seekto(intpositionwhenpause);
   //初始播放位置
   intpositionwhenpause = -1;
  }
 }
}

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