1. 程式人生 > 程式設計 >Android原生視訊播放VideoView的使用

Android原生視訊播放VideoView的使用

本文例項為大家分享了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;
  }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。