The use of Android native video playback VideoView
- 2021-12-04 11:27:57
- OfStack
In this paper, we share the specific code of Android native video playing VideoView for your reference. The specific contents are as follows
Layout file 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>
Corresponding 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);
// Call the system's own video playback or installation 3 Square player
// 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();
}
/**
* Initialization videoview Play
*/
public void initVideoView() {
// Initialize progress bar
progressBar = (ProgressBar) findViewById(R.id.progressBar);
// Initialization VideoView
videoView = (VideoView) findViewById(R.id.videoView);
// Initialization videoview Control bar
mediaController = new MediaController(this);
// Settings videoview Control bar of
videoView.setMediaController(mediaController);
// Set the display control bar
mediaController.show(0);
// Set up listening after playback is completed
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
// Set error listening, if you do not set videoview The user is prompted that an error has occurred
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
// Set the callback function after the video file is loaded
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressBar.setVisibility(View.GONE);
videoView.start();
}
});
// Settings videoView Click listening of
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
// Set up the network video path
Uri uri = Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4");
videoView.setVideoURI(uri);
// Set to play in full screen mode
setVideoViewLayoutParams(2);
}
/**
* Settings videiview Full screen and window modes of
*
* @param paramsType Identification 1 For full screen mode 2 For window mode
*/
public void setVideoViewLayoutParams(int paramsType) {
// Full screen mode
if (1 == paramsType) {
// Set to fill the entire parent layout
RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
// Set relative to the parent layout 4 Edge alignment
LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// For VideoView Add Attributes
videoView.setLayoutParams(LayoutParams);
} else {
// Window mode
// Gets the width and height of the entire screen
DisplayMetrics DisplayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(DisplayMetrics);
// Set the window mode distance border 50
int videoHeight = DisplayMetrics.heightPixels;
int videoWidth = DisplayMetrics.widthPixels;
RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth, videoHeight);
// Set center
LayoutParams.addRule(RelativeLayout.ALIGN_TOP);
// For VideoView Add Attributes
videoView.setLayoutParams(LayoutParams);
}
}
/**
* Page pause effect processing
*/
@Override
protected void onPause() {
super.onPause();
// If the current page is paused, the current playback position is saved, and the global variable is saved
intPositionWhenPause = videoView.getCurrentPosition();
// Stop playing back video files
videoView.stopPlayback();
}
/**
* Page resumes from pause
*/
@Override
protected void onResume() {
super.onResume();
// Jump to the location saved when paused
if (intPositionWhenPause >= 0) {
videoView.seekTo(intPositionWhenPause);
// Initial playback position
intPositionWhenPause = -1;
}
}
}