-
Notifications
You must be signed in to change notification settings - Fork 246
scaleVideo
xinbaicheng edited this page Sep 14, 2016
·
10 revisions
播放器类KSYMediaPlayer提供接口 setVideoScalingMode,可以设置视频伸缩模式
KSYMediaPlayer player = KSYMediaPlayer.Builder(mContext).build();
// 填充模式
player.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
// 裁剪模式
player.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
调用时机:
- onPrepared回调时
- 屏幕旋转时
VideoSurfaceView集成了SurfaceView,使用硬解时可在布局中使用VideoSurfaceView,下面为VideoSurfaceView的实现
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;
/**
* Created by xbc on 16/3/2.
* @author [email protected]
*/
public class VideoSurfaceView extends SurfaceView {
private int mVideoWidth;
private int mVideoHeight;
private int mMeasuredWidth;
private int mMeasuredHeight;
public VideoSurfaceView(Context context) {
super(context);
}
public VideoSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
doMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(mMeasuredWidth, mMeasuredHeight);
}
public void setVideoDimension(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
}
private void doMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = View.getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = View.getDefaultSize(mVideoHeight, heightMeasureSpec);
if(mVideoWidth > 0 && mVideoHeight > 0)
{
int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = View.MeasureSpec.getSize(heightMeasureSpec);
float specAspectRatio = (float) widthSpecSize / (float) heightSpecSize;
float displayAspectRatio = (float) mVideoWidth / (float) mVideoHeight;
boolean shouldBeWider = displayAspectRatio > specAspectRatio;
if (shouldBeWider) {
// not high enough, fix height
height = heightSpecSize;
width = (int) (height * displayAspectRatio);
} else {
// not wide enough, fix width
width = widthSpecSize;
height = (int) (width / displayAspectRatio);
}
}
mMeasuredWidth = width;
mMeasuredHeight = height;
}
}
播放器的部分布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/player_black">
<VideoSurfaceView
android:id="@+id/player_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
播放器的部分代码
private VideoSurfaceView mVideoSurfaceView;
mVideoSurfaceView = (VideoSurfaceView) findViewById(R.id.player_surface);
private IMediaPlayer.OnPreparedListener mOnPreparedListener = new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
if (ksyMediaPlayer != null) {
if(mVideoSurfaceView != null)
{
mVideoSurfaceView.setVideoDimension(ksyMediaPlayer.getVideoWidth(), ksyMediaPlayer.getVideoHeight());
mVideoSurfaceView.requestLayout();
}
ksyMediaPlayer.start();
}
}