Skip to content

Commit 18576d6

Browse files
committed
优化 ijk 模式的视频尺寸计算
1 parent d9e82c5 commit 18576d6

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

app/src/main/java/com/example/gsyvideoplayer/DetailPlayer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ private GSYVideoPlayer getCurPlay() {
254254

255255
private String getUrl() {
256256

257-
///String url = "android.resource://" + getPackageName() + "/" + R.raw.test1;
257+
//String url = "android.resource://" + getPackageName() + "/" + R.raw.test1;
258258
//注意,用ijk模式播放raw视频,这个必须打开
259-
///GSYVideoManager.instance().enableRawPlay(getApplicationContext());
259+
//GSYVideoManager.instance().enableRawPlay(getApplicationContext());
260260

261261
///exo raw 支持
262-
///String url = RawResourceDataSource.buildRawResourceUri(R.raw.test).toString();
262+
//String url = RawResourceDataSource.buildRawResourceUri(R.raw.test).toString();
263263

264264
//断网自动重新链接,url前接上ijkhttphook:
265265
//String url = "ijkhttphook:https://res.exexm.com/cw_145225549855002";
@@ -302,6 +302,7 @@ private String getUrl() {
302302
//String url = "http://hls.ciguang.tv/hdtv/video.m3u8";
303303
//String url = "https://res.exexm.com/cw_145225549855002";
304304
//String url = "http://storage.gzstv.net/uploads/media/huangmeiyan/jr05-09.mp4";//mepg
305+
//String url = "https://zh-files.oss-cn-qingdao.aliyuncs.com/20170808223928mJ1P3n57.mp4";//90度
305306
return url;
306307
}
307308
}

gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/utils/MeasureHelper.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ public void doMeasure(int widthMeasureSpec, int heightMeasureSpec) {
8787
heightMeasureSpec = tempSpec;
8888
}
8989

90-
int width = View.getDefaultSize(mVideoWidth, widthMeasureSpec);
90+
int realWidth = mVideoWidth;
91+
92+
if(mVideoSarNum != 0 && mVideoSarDen != 0) {
93+
double pixelWidthHeightRatio = mVideoSarNum / (mVideoSarDen / 1.0);
94+
realWidth = (int) (pixelWidthHeightRatio * mVideoWidth);
95+
}
96+
97+
int width = View.getDefaultSize(realWidth, widthMeasureSpec);
9198
int height = View.getDefaultSize(mVideoHeight, heightMeasureSpec);
9299
if (mCurrentAspectRatio == GSYVideoType.SCREEN_MATCH_FULL) {
93100
width = widthMeasureSpec;
94101
height = heightMeasureSpec;
95-
} else if (mVideoWidth > 0 && mVideoHeight > 0) {
102+
} else if (realWidth > 0 && mVideoHeight > 0) {
96103
int widthSpecMode = View.MeasureSpec.getMode(widthMeasureSpec);
97104
int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec);
98105
int heightSpecMode = View.MeasureSpec.getMode(heightMeasureSpec);
@@ -126,7 +133,7 @@ public void doMeasure(int widthMeasureSpec, int heightMeasureSpec) {
126133
case GSYVideoType.SCREEN_TYPE_FULL:
127134
//case GSYVideoType.AR_ASPECT_WRAP_CONTENT:
128135
default:
129-
displayAspectRatio = (float) mVideoWidth / (float) mVideoHeight;
136+
displayAspectRatio = (float) realWidth / (float) mVideoHeight;
130137
if (mVideoSarNum > 0 && mVideoSarDen > 0)
131138
displayAspectRatio = displayAspectRatio * mVideoSarNum / mVideoSarDen;
132139
break;
@@ -164,7 +171,7 @@ public void doMeasure(int widthMeasureSpec, int heightMeasureSpec) {
164171
default:
165172
if (shouldBeWider) {
166173
// too wide, fix width
167-
width = Math.min(mVideoWidth, widthSpecSize);
174+
width = Math.min(realWidth, widthSpecSize);
168175
height = (int) (width / displayAspectRatio);
169176
} else {
170177
// too high, fix height
@@ -179,42 +186,42 @@ public void doMeasure(int widthMeasureSpec, int heightMeasureSpec) {
179186
height = heightSpecSize;
180187

181188
// for compatibility, we adjust size based on aspect ratio
182-
if (mVideoWidth * height < width * mVideoHeight) {
189+
if (realWidth * height < width * mVideoHeight) {
183190
//Log.i("@@@", "image too wide, correcting");
184-
width = height * mVideoWidth / mVideoHeight;
185-
} else if (mVideoWidth * height > width * mVideoHeight) {
191+
width = height * realWidth / mVideoHeight;
192+
} else if (realWidth * height > width * mVideoHeight) {
186193
//Log.i("@@@", "image too tall, correcting");
187-
height = width * mVideoHeight / mVideoWidth;
194+
height = width * mVideoHeight / realWidth;
188195
}
189196
} else if (widthSpecMode == View.MeasureSpec.EXACTLY) {
190197
// only the width is fixed, adjust the height to match aspect ratio if possible
191198
width = widthSpecSize;
192-
height = width * mVideoHeight / mVideoWidth;
199+
height = width * mVideoHeight / realWidth;
193200
if (heightSpecMode == View.MeasureSpec.AT_MOST && height > heightSpecSize) {
194201
// couldn't match aspect ratio within the constraints
195202
height = heightSpecSize;
196203
}
197204
} else if (heightSpecMode == View.MeasureSpec.EXACTLY) {
198205
// only the height is fixed, adjust the width to match aspect ratio if possible
199206
height = heightSpecSize;
200-
width = height * mVideoWidth / mVideoHeight;
207+
width = height * realWidth / mVideoHeight;
201208
if (widthSpecMode == View.MeasureSpec.AT_MOST && width > widthSpecSize) {
202209
// couldn't match aspect ratio within the constraints
203210
width = widthSpecSize;
204211
}
205212
} else {
206213
// neither the width nor the height are fixed, try to use actual video size
207-
width = mVideoWidth;
214+
width = realWidth;
208215
height = mVideoHeight;
209216
if (heightSpecMode == View.MeasureSpec.AT_MOST && height > heightSpecSize) {
210217
// too tall, decrease both width and height
211218
height = heightSpecSize;
212-
width = height * mVideoWidth / mVideoHeight;
219+
width = height * realWidth / mVideoHeight;
213220
}
214221
if (widthSpecMode == View.MeasureSpec.AT_MOST && width > widthSpecSize) {
215222
// too wide, decrease both width and height
216223
width = widthSpecSize;
217-
height = width * mVideoHeight / mVideoWidth;
224+
height = width * mVideoHeight / realWidth;
218225
}
219226
}
220227
} else {

0 commit comments

Comments
 (0)