MediaPlayer+TextureView实现视频播放功能

前面写一些基础知识的学习,这篇写个小demo,实现视频循环播放功能。
1、xml代码:

 

2、初始化视频资源,这里把需要播放的视频集成在应用中,位置放在res/raw下。

private void initVideoUri() {
    mVideoUris = new ArrayList<>();
    mVideoUris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video1));
    mVideoUris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video2));
    mVideoUris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3));
}

3、初始化TextureView,并设置监听,需求需要给给TextureView加一个圆角,角度是16。

private void initTextureView() {
    mTextureView = findViewById(R.id.textureView);
    //加圆角
    mTextureView.setOutlineProvider(new TextureVideoViewOutlineProvider(16));
    mTextureView.setClipToOutline(true);
    mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            // SurfaceTexture准备就绪,初始化MediaPlayer
            initPlayer(surfaceTexture);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            // SurfaceTexture缓冲大小变化
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            // SurfaceTexture即将被销毁
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            // SurfaceTexture通过updateImage更新
       }
    });
}

圆角类:

public class TextureVideoViewOutlineProvider extends ViewOutlineProvider {
        private float mRadius;
     
        public TextureVideoViewOutlineProvider(float radius) {
            this.mRadius = radius;
        }
     
        @Override
        public void getOutline(View view, Outline outline) {
            Rect rect = new Rect();
            view.getGlobalVisibleRect(rect);
            int leftMargin = 0;
            int topMargin = 0;
            Rect selfRect = new Rect(leftMargin, topMargin,
                    rect.right - rect.left - leftMargin, rect.bottom - rect.top - topMargin);
            outline.setRoundRect(selfRect, mRadius);
        }
    }

4、初始化MediaPalyer播放器

private void initPlayer(SurfaceTexture surfaceTexture) {
    try {
        mPlayer = new MediaPlayer();
        mPlayer.setSurface(new Surface(surfaceTexture));
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setDataSource(mContext, mVideoUris.get(0));
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //这里可以调用start()方法开始播放视频      
                mPlayer.start(); 
            }
        }
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                  //循环播放列表中的内容
                  if (mPlayIndex < mVideoUris.size() - 1) {
                        mPlayIndex = ++mPlayIndex;
                  } else {
                        mPlayIndex = 0;
                  }
                  Log.e(TAG, "onCompletion - mPlayIndex: " + mPlayIndex);
                  playVideo(mVideoUris.get(index));
            }
        }

        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
          @Override
          public boolean onError(MediaPlayer mp, int what, int extra) {
              Log.e(TAG, "onError - what: " + what + " | extra: " + extra);
              return false;
          }
        }

        mPlayer.prepareAsync();
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
}   

5、视频播放

private void playVideo(Uri uri) {
    try {
        mPlayer.reset();
        mPlayer.setDataSource(mContext, uri);
        mPlayer.prepareAsync();
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }

6、资源释放

private void releaseMediaPlayer() {
    if (mPlayer != null) {
        if (mPlayer.isPlaying()) {
            mPlayer..stop();
        }
       mPlayer.release();
    }
}

这个demo只是简单实现,具体播放最好放子线程进行。

如何实现TextureView或者SurfaceView 预览框为圆角

你可能感兴趣的:(MediaPlayer+TextureView实现视频播放功能)