提交 d25bd553 编写于 作者: S shuyu

增加了SurfaceView的支持 (2017-08-26)

上级 32c084ec
......@@ -26,5 +26,6 @@ public class GSYApplication extends Application {
//GSYVideoManager.instance().setVideoType(this, GSYVideoType.IJKEXOPLAYER);
//GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL);
//GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL);
//GSYVideoType.setRenderType(GSYVideoType.SUFRACE);
}
}
package com.shuyu.gsyvideoplayer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
/**
* render绘制中间控件
* Created by guoshuyu on 2017/8/26.
*/
public class GSYRenderView {
private View mShowView;
public void setTransform(Matrix transform) {
if (mShowView instanceof TextureView) {
((TextureView) mShowView).setTransform(transform);
}
}
public void requestLayout() {
mShowView.requestLayout();
}
public float getRotation() {
return mShowView.getRotation();
}
public void setRotation(float rotation) {
mShowView.setRotation(rotation);
}
public void invalidate() {
mShowView.invalidate();
}
public int getWidth() {
return mShowView.getWidth();
}
public int getHeight() {
return mShowView.getHeight();
}
/**
* 暂停时初始化位图
*/
public Bitmap initCover() {
if (mShowView != null && mShowView instanceof GSYTextureView) {
GSYTextureView textureView = (GSYTextureView) mShowView;
Bitmap bitmap = Bitmap.createBitmap(
textureView.getSizeW(), textureView.getSizeH(), Bitmap.Config.RGB_565);
return textureView.getBitmap(bitmap);
}
return null;
}
public ViewGroup.LayoutParams getLayoutParams() {
return mShowView.getLayoutParams();
}
public void setLayoutParams(ViewGroup.LayoutParams layoutParams) {
mShowView.setLayoutParams(layoutParams);
}
/**
* 添加播放的view
*/
public void addTextureView(Context context, ViewGroup textureViewContainer, int rotate, TextureView.SurfaceTextureListener listener) {
if (textureViewContainer.getChildCount() > 0) {
textureViewContainer.removeAllViews();
}
GSYTextureView gsyTextureView = new GSYTextureView(context);
gsyTextureView.setSurfaceTextureListener(listener);
gsyTextureView.setRotation(rotate);
mShowView = gsyTextureView;
int params = getTextureParams();
if (textureViewContainer instanceof RelativeLayout) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params, params);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textureViewContainer.addView(gsyTextureView, layoutParams);
} else if (textureViewContainer instanceof FrameLayout) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(params, params);
layoutParams.gravity = Gravity.CENTER;
textureViewContainer.addView(gsyTextureView, layoutParams);
}
}
/**
* 添加播放的view
*/
public void addSurfaceView(Context context, ViewGroup textureViewContainer, int rotate, SurfaceHolder.Callback2 callback2) {
if (textureViewContainer.getChildCount() > 0) {
textureViewContainer.removeAllViews();
}
GSYSurfaceView showSurfaceView = new GSYSurfaceView(context);
showSurfaceView.getHolder().addCallback(callback2);
showSurfaceView.setRotation(rotate);
mShowView = showSurfaceView;
int params = getTextureParams();
if (textureViewContainer instanceof RelativeLayout) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params, params);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textureViewContainer.addView(showSurfaceView, layoutParams);
} else if (textureViewContainer instanceof FrameLayout) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(params, params);
layoutParams.gravity = Gravity.CENTER;
textureViewContainer.addView(showSurfaceView, layoutParams);
}
}
/**
* 获取布局参数
*
* @return
*/
protected int getTextureParams() {
boolean typeChanged = (GSYVideoType.getShowType() != GSYVideoType.SCREEN_TYPE_DEFAULT);
return (typeChanged) ? ViewGroup.LayoutParams.WRAP_CONTENT : ViewGroup.LayoutParams.MATCH_PARENT;
}
}
package com.shuyu.gsyvideoplayer;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;
import com.shuyu.gsyvideoplayer.utils.MeasureHelper;
/**
*
* Created by guoshuyu on 2017/8/26.
*/
public class GSYSurfaceView extends SurfaceView {
private MeasureHelper measureHelper;
public GSYSurfaceView(Context context) {
super(context);
init();
}
public GSYSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
measureHelper = new MeasureHelper(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int videoWidth = GSYVideoManager.instance().getCurrentVideoWidth();
int videoHeight = GSYVideoManager.instance().getCurrentVideoHeight();
int videoSarNum = GSYVideoManager.instance().getMediaPlayer().getVideoSarNum();
int videoSarDen = GSYVideoManager.instance().getMediaPlayer().getVideoSarDen();
if (videoWidth > 0 && videoHeight > 0) {
measureHelper.setVideoSampleAspectRatio(videoSarNum, videoSarDen);
measureHelper.setVideoSize(videoWidth, videoHeight);
}
measureHelper.setVideoRotation((int)getRotation());
measureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureHelper.getMeasuredWidth(), measureHelper.getMeasuredHeight());
}
public int getSizeH() {
return measureHelper.getMeasuredHeight();
}
public int getSizeW() {
return measureHelper.getMeasuredWidth();
}
}
......@@ -26,6 +26,11 @@ public class GSYVideoType {
public final static int IJKEXOPLAYER = 1;
//surface
public final static int SUFRACE = 1;
//texture
public final static int TEXTURE = 0;
//显示比例类型
......@@ -35,6 +40,8 @@ public class GSYVideoType {
private static boolean MEDIA_CODEC_FLAG = false;
private static int sRenderType = TEXTURE;
/**
* 使能硬解码,播放前设置
......@@ -67,4 +74,18 @@ public class GSYVideoType {
public static void setShowType(int type) {
TYPE = type;
}
public static int getRenderType() {
return sRenderType;
}
/**
* 渲染控件
*
* @param renderType
*/
public static void setRenderType(int renderType) {
sRenderType = renderType;
}
}
......@@ -9,11 +9,16 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.shuyu.gsyvideoplayer.GSYRenderView;
import com.shuyu.gsyvideoplayer.GSYSurfaceView;
import com.shuyu.gsyvideoplayer.GSYTextureView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
......@@ -23,13 +28,13 @@ import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
* Created by guoshuyu on 2017/8/2.
*/
public abstract class GSYTextureRenderView extends FrameLayout implements TextureView.SurfaceTextureListener {
public abstract class GSYTextureRenderView extends FrameLayout implements TextureView.SurfaceTextureListener, SurfaceHolder.Callback2 {
//native绘制
protected Surface mSurface;
//渲染控件
protected GSYTextureView mTextureView;
protected GSYRenderView mTextureView;
//渲染控件父类
protected ViewGroup mTextureViewContainer;
......@@ -52,15 +57,12 @@ public abstract class GSYTextureRenderView extends FrameLayout implements Textur
super(context, attrs, defStyleAttr);
}
/******************** TextureView ****************************/
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurface = new Surface(surface);
//显示暂停切换显示的图片
showPauseCover();
GSYVideoManager.instance().setDisplay(mSurface);
Surface newSurface = new Surface(surface);
pauseLogic(newSurface, true);
}
@Override
......@@ -82,33 +84,58 @@ public abstract class GSYTextureRenderView extends FrameLayout implements Textur
releasePauseCover();
}
/******************** SurfaceView ****************************/
@Override
public void surfaceCreated(SurfaceHolder holder) {
pauseLogic(holder.getSurface(), false);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//清空释放
GSYVideoManager.instance().setDisplay(null);
holder.getSurface().release();
}
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
}
/**
* 暂停逻辑
*/
protected void pauseLogic(Surface surface, boolean pauseLogic) {
mSurface = surface;
if (pauseLogic)
//显示暂停切换显示的图片
showPauseCover();
GSYVideoManager.instance().setDisplay(mSurface);
}
/**
* 添加播放的view
*/
protected void addTextureView() {
if (mTextureViewContainer.getChildCount() > 0) {
mTextureViewContainer.removeAllViews();
}
mTextureView = null;
mTextureView = new GSYTextureView(getContext());
mTextureView.setSurfaceTextureListener(this);
mTextureView.setRotation(mRotate);
int params = getTextureParams();
mTextureView = new GSYRenderView();
if (mTextureViewContainer instanceof RelativeLayout) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params, params);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mTextureViewContainer.addView(mTextureView, layoutParams);
} else if (mTextureViewContainer instanceof FrameLayout) {
LayoutParams layoutParams = new LayoutParams(params, params);
layoutParams.gravity = Gravity.CENTER;
mTextureViewContainer.addView(mTextureView, layoutParams);
if (GSYVideoType.getRenderType() == GSYVideoType.SUFRACE) {
mTextureView.addSurfaceView(getContext(), mTextureViewContainer, mRotate, this);
return;
}
mTextureView.addTextureView(getContext(), mTextureViewContainer, mRotate, this);
}
/**
* 获取布局参数
*
* @return
*/
protected int getTextureParams() {
......@@ -131,10 +158,7 @@ public abstract class GSYTextureRenderView extends FrameLayout implements Textur
* 暂停时初始化位图
*/
protected void initCover() {
Bitmap bitmap = Bitmap.createBitmap(
mTextureView.getSizeW(), mTextureView.getSizeH(), Bitmap.Config.RGB_565);
mFullPauseBitmap = mTextureView.getBitmap(bitmap);
//bitmap.recycle();
mFullPauseBitmap = mTextureView.initCover();
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册