TextureView播放本地视频

耗尽温柔 提交于 2020-01-18 03:56:10

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 onError(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!