本文主要是介绍MediaPlayer+TextureView实现视频播放功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前面写一些基础知识的学习,这篇写个小demo,实现视频循环播放功能。
1、xml代码:
<TextureViewandroid:id="@+id/textureView"android:layout_width="600px"android:layout_height="400px"android:focusable="false" />
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() {@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {// SurfaceTexture准备就绪,初始化MediaPlayerinitPlayer(surfaceTexture);}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {// SurfaceTexture缓冲大小变化}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {// SurfaceTexture即将被销毁return false;}@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface) {// SurfaceTexture通过updateImage更新}});
}
圆角类:
public class TextureVideoViewOutlineProvider extends ViewOutlineProvider {private float mRadius;public TextureVideoViewOutlineProvider(float radius) {this.mRadius = radius;}@Overridepublic 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() {@Overridepublic void onPrepared(MediaPlayer mp) {//这里可以调用start()方法开始播放视频 mPlayer.start(); }}mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Overridepublic 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() {@Overridepublic 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实现视频播放功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!