1.效果图:
2.清单文件:文件读取权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.主界面:
package com.example.administrator.testz;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnVideoSizeChangedListener {
private String videoPath = Environment.getExternalStorageDirectory().getPath()+"/video.mp4";
private TextureView textureView;
private MediaPlayer mediaPlayer;
private SurfaceTexture surfaceTexture;
private Surface surface;
private final static String TAG = "MainActivity";
private int mVideomode=0;
private int mVideoWidth;
private int mVideoHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = findViewById(R.id.id_textureview);
textureView.setSurfaceTextureListener(this);
}
@Override
protected void onResume() {
super.onResume();
if (textureView.isAvailable()){
playVideo();
}
}
@Override
protected void onPause() {
super.onPause();
if (surfaceTexture != null){
surfaceTexture.release();
}
if (mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.e("TAG","Buffering:"+percent);
}
@Override
public boolean one rror(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
if (mp!=null){
mediaPlayer.setVolume(1f,1f);
mediaPlayer.start();
}
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
mVideoWidth = mp.getVideoWidth();
mVideoHeight = mp.getVideoHeight();
updateTextureViewSizeCenter();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
playVideo();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
updateTextureViewSizeCenter();
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
private void updateTextureViewSizeCenter() {
float sx = (float)textureView.getWidth() / (float)mVideoWidth;
float sy = (float)textureView.getHeight() / (float)mVideoHeight;
Matrix matrix = new Matrix();
matrix.preTranslate((textureView.getWidth() - mVideoWidth) / 2, (textureView.getHeight() - mVideoHeight) / 2);
matrix.preScale(mVideoWidth / (float)textureView.getWidth(), mVideoHeight / (float)textureView.getHeight());
if (sx >= sy) {
matrix.postScale(sy, sy, textureView.getWidth() / 2, textureView.getHeight() / 2);
} else {
matrix.postScale(sx, sx, textureView.getWidth() / 2, textureView.getHeight() / 2);
}
textureView.setTransform(matrix);
textureView.postInvalidate();
//放大三倍
matrix.setScale(1 + 0 / 100f, 1 + 0 / 100f);
textureView.setTransform(matrix);
}
private void playVideo() {
if (mediaPlayer == null){
surfaceTexture = textureView.getSurfaceTexture();
surface = new Surface(surfaceTexture);
initMediaPlayer();
}
}
private void initMediaPlayer() {
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(videoPath);
mediaPlayer.setSurface(surface);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setLooping(true);
}catch (IOException e){
e.printStackTrace();
}
}
}
4.布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:layout_alignParentRight="true"
android:id="@+id/id_textureview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
end
cf8833 发布了167 篇原创文章 · 获赞 20 · 访问量 7万+ 私信 关注