Skip to content

Commit

Permalink
add 视频全屏播放时,横竖屏可自己处理
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Aug 29, 2022
1 parent 6019755 commit 1a3320e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
16 changes: 11 additions & 5 deletions ByWebView/src/main/java/me/jingbin/web/ByWebChromeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
*/
public class ByWebChromeClient extends WebChromeClient {

private WeakReference<Activity> mActivityWeakReference = null;
private WeakReference<Activity> mActivityWeakReference;
private ByWebView mByWebView;
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mUploadMessageForAndroid5;
private static int RESULT_CODE_FILE_CHOOSER = 1;
private static int RESULT_CODE_FILE_CHOOSER_FOR_ANDROID_5 = 2;
private static final int RESULT_CODE_FILE_CHOOSER = 1;
private static final int RESULT_CODE_FILE_CHOOSER_FOR_ANDROID_5 = 2;

private View mProgressVideo;
private View mCustomView;
Expand Down Expand Up @@ -73,7 +73,10 @@ public void onShowCustomView(View view, CustomViewCallback callback) {
Activity mActivity = this.mActivityWeakReference.get();
if (mActivity != null && !mActivity.isFinishing()) {
if (!isFixScreenLandscape) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (onByWebChromeCallback == null || !onByWebChromeCallback.onHandleScreenOrientation(true)) {
// 为空或返回为ture时,自己处理横竖屏。否则全屏时默认设置为横屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
mByWebView.getWebView().setVisibility(View.INVISIBLE);

Expand Down Expand Up @@ -108,7 +111,10 @@ public void onHideCustomView() {
}
// 还原到之前的屏幕状态
if (!isFixScreenPortrait) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (onByWebChromeCallback == null || !onByWebChromeCallback.onHandleScreenOrientation(false)) {
// 为空或返回为ture时,自己处理横竖屏。否则默认设置为竖屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}

mCustomView.setVisibility(View.GONE);
Expand Down
3 changes: 1 addition & 2 deletions ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
if (onByWebClientCallback == null || !onByWebClientCallback.onReceivedSslError(view, handler, error)) {
// 默认http请求会有弹框提示,如果要自己处理需要使用onReceivedSslError()且返回true
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage("SSL认证失败,是否继续访问?");
builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
Expand All @@ -153,8 +154,6 @@ public void onClick(DialogInterface dialog, int which) {
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
onByWebClientCallback.onReceivedSslError(view, handler, error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@
*/
public abstract class OnTitleProgressCallback {

/**
* @param title 返回的标题
*/
public void onReceivedTitle(String title) {

}

/**
* @param newProgress 返回的进度
*/
public void onProgressChanged(int newProgress) {

}

/**
* 全屏显示时处理横竖屏。
* 默认返回false,全屏时为横屏,全屏还原后为竖屏
* 如果要手动处理,需要返回true!
*
* @param isShow 是否显示了全屏视频 ture点击了全屏显示,false全屏视频还原
*/
public boolean onHandleScreenOrientation(boolean isShow) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
Expand All @@ -21,7 +16,11 @@
import android.widget.TextView;
import android.widget.Toast;

import com.example.jingbin.webviewstudy.App;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.example.jingbin.webviewstudy.MainActivity;
import com.example.jingbin.webviewstudy.R;
import com.example.jingbin.webviewstudy.config.MyJavascriptInterface;
Expand Down Expand Up @@ -116,6 +115,17 @@ public void onReceivedTitle(String title) {
Log.e("---title", title);
tvGunTitle.setText(title);
}

// // 视频全屏时,横竖屏自己处理
// @Override
// public boolean onHandleScreenOrientation(boolean isShow) {
// if (isShow) {
// ByWebViewActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// } else {
// ByWebViewActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// }
// return true;
// }
};

private OnByWebClientCallback onByWebClientCallback = new OnByWebClientCallback() {
Expand Down Expand Up @@ -235,7 +245,7 @@ private void loadWebsiteSourceCodeJs() {
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode,resultCode,intent);
super.onActivityResult(requestCode, resultCode, intent);
byWebView.handleFileChooser(requestCode, resultCode, intent);
}

Expand Down

0 comments on commit 1a3320e

Please sign in to comment.