-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
harry
committed
Jan 11, 2019
1 parent
1905fbd
commit be777ac
Showing
7 changed files
with
247 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 20 additions & 76 deletions
96
app/src/main/java/com/example/harry/myapplication/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,37 @@ | ||
package com.example.harry.myapplication; | ||
|
||
import android.content.pm.ActivityInfo; | ||
import android.support.constraint.ConstraintLayout; | ||
import android.support.v7.app.ActionBar; | ||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.Window; | ||
import android.view.WindowManager; | ||
import android.webkit.WebChromeClient; | ||
import android.webkit.WebSettings; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
|
||
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
private EditText url_text; | ||
public static final String URL_MESSAGE = "com.example.harry.myapplication.URL_MESSAGE"; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
private final String TAG = "MainActivity"; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
final ConstraintLayout customViewContainer = findViewById(R.id.layout); | ||
final WebView myWebView = (WebView) findViewById(R.id.webview); | ||
final AppCompatActivity curActivity = this; | ||
|
||
myWebView.setWebChromeClient(new WebChromeClient(){ | ||
|
||
private CustomViewCallback exitFulscreenFunc; | ||
private View customView; | ||
|
||
@Override | ||
public void onShowCustomView(View view, CustomViewCallback callback) { | ||
super.onShowCustomView(view, callback); | ||
Log.d(TAG, "onShowCustomView: "); | ||
view.setLayoutParams(new ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT | ||
)); | ||
((MainActivity) curActivity).hideTitleBar(); | ||
myWebView.setVisibility(View.GONE); | ||
customViewContainer.setVisibility(View.VISIBLE); | ||
customViewContainer.addView(view); | ||
exitFulscreenFunc = callback; | ||
customView = view; | ||
curActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | ||
} | ||
|
||
@Override | ||
public void onHideCustomView() { | ||
super.onHideCustomView(); | ||
Log.d(TAG, "onHideCustomView: "); | ||
((MainActivity) curActivity).showTitleBar(); | ||
myWebView.setVisibility(View.VISIBLE); | ||
customViewContainer.removeView(customView); | ||
exitFulscreenFunc.onCustomViewHidden(); | ||
curActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | ||
} | ||
}); | ||
|
||
myWebView.setWebViewClient(new WebViewClient()); | ||
myWebView.loadUrl("http://mudu.tv/?c=activity&a=live&id=156450"); | ||
WebSettings webSettings = myWebView.getSettings(); | ||
webSettings.setJavaScriptEnabled(true); | ||
webSettings.setDomStorageEnabled(true); | ||
url_text = (EditText) findViewById(R.id.url_text); | ||
Button entry_button = (Button) findViewById(R.id.entry_button); | ||
entry_button.setOnClickListener(this); | ||
} | ||
|
||
public void hideTitleBar () { | ||
// requestWindowFeature(Window.FEATURE_NO_TITLE); | ||
View decorView = getWindow().getDecorView(); | ||
// int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; | ||
decorView.setSystemUiVisibility( | ||
View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | ||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // hide status bar and nav bar after a short delay, or if the user interacts with the middle of the screen | ||
); | ||
ActionBar actionBar = getSupportActionBar(); | ||
actionBar.hide(); | ||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | ||
} | ||
|
||
public void showTitleBar () { | ||
// requestWindowFeature(Window.FEATURE_ACTION_BAR); | ||
View decorView = getWindow().getDecorView(); | ||
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; | ||
decorView.setSystemUiVisibility(uiOptions); | ||
ActionBar actionBar = getSupportActionBar(); | ||
actionBar.show(); | ||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | ||
@Override | ||
public void onClick(View v) { | ||
switch (v.getId()) { | ||
case R.id.entry_button: | ||
String url = url_text.getText().toString(); | ||
Intent intent = new Intent(this, MuduRoom.class); | ||
intent.putExtra(URL_MESSAGE, url); | ||
startActivity(intent); | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
app/src/main/java/com/example/harry/myapplication/MuduRoom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.example.harry.myapplication; | ||
|
||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.support.constraint.ConstraintLayout; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.WindowManager; | ||
import android.webkit.WebChromeClient; | ||
import android.webkit.WebSettings; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
|
||
public class MuduRoom extends AppCompatActivity { | ||
|
||
private final String TAG = "MuduRoom"; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_mudu_room); | ||
|
||
final ConstraintLayout customViewContainer = findViewById(R.id.layout); | ||
final WebView myWebView = (WebView) findViewById(R.id.webview); | ||
final AppCompatActivity curActivity = this; | ||
|
||
// 获取传过来的html页面地址 | ||
Intent intent = getIntent(); | ||
String url = intent.getStringExtra(MainActivity.URL_MESSAGE); | ||
|
||
myWebView.setWebChromeClient(new WebChromeClient(){ | ||
|
||
private CustomViewCallback exitFulscreenFunc; | ||
private View customView; | ||
|
||
@Override | ||
public void onShowCustomView(View view, CustomViewCallback callback) { | ||
super.onShowCustomView(view, callback); | ||
Log.d(TAG, "onShowCustomView: "); | ||
|
||
// 全屏view是framelayout,需要设置它的layoutParams为match_parent,否则有可能全屏后下面出现空白 | ||
view.setLayoutParams(new ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT | ||
)); | ||
|
||
// 隐藏title Bar及status bar | ||
((MuduRoom) curActivity).hideTitleBar(); | ||
|
||
// 将原来的webView隐藏 | ||
myWebView.setVisibility(View.GONE); | ||
|
||
// 将全屏view设为可见,并添加到页面中 | ||
customViewContainer.setVisibility(View.VISIBLE); | ||
customViewContainer.addView(view); | ||
|
||
// 保存退出全屏的方法 | ||
exitFulscreenFunc = callback; | ||
|
||
// 保存全屏view | ||
customView = view; | ||
|
||
// 设置屏幕为横屏 | ||
curActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | ||
} | ||
|
||
@Override | ||
public void onHideCustomView() { | ||
super.onHideCustomView(); | ||
Log.d(TAG, "onHideCustomView: "); | ||
|
||
// 显示title bar及status bar | ||
((MuduRoom) curActivity).showTitleBar(); | ||
|
||
// 将原来的webview设为可见 | ||
myWebView.setVisibility(View.VISIBLE); | ||
|
||
// 移除全屏view | ||
customViewContainer.removeView(customView); | ||
|
||
// 调用退出全屏的方法 | ||
exitFulscreenFunc.onCustomViewHidden(); | ||
|
||
// 设置屏幕为竖屏 | ||
curActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | ||
} | ||
}); | ||
|
||
// 设置WebViewClient,防止使用浏览器打开 | ||
myWebView.setWebViewClient(new WebViewClient()); | ||
// 加载html页面地址 | ||
myWebView.loadUrl(url); | ||
|
||
// 设置允许javascript及domStorage | ||
WebSettings webSettings = myWebView.getSettings(); | ||
webSettings.setJavaScriptEnabled(true); | ||
webSettings.setDomStorageEnabled(true); | ||
} | ||
|
||
public void hideTitleBar () { | ||
|
||
// 设置全屏及status bar自动隐藏 | ||
View decorView = getWindow().getDecorView(); | ||
decorView.setSystemUiVisibility( | ||
View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | ||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // hide status bar and nav bar after a short delay, or if the user interacts with the middle of the screen | ||
); | ||
|
||
// 隐藏actionBar | ||
ActionBar actionBar = getSupportActionBar(); | ||
actionBar.hide(); | ||
|
||
// 设置屏幕常亮,全屏后过一段时间屏幕可能会变暗 | ||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | ||
} | ||
|
||
public void showTitleBar () { | ||
// 显示status bar | ||
View decorView = getWindow().getDecorView(); | ||
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); | ||
|
||
// 显示actionBar | ||
ActionBar actionBar = getSupportActionBar(); | ||
actionBar.show(); | ||
|
||
// 取消屏幕常亮 | ||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/layout" | ||
android:orientation="vertical" | ||
tools:context=".MainActivity"> | ||
|
||
<WebView | ||
android:id="@+id/webview" | ||
|
||
<EditText | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/url_text" | ||
android:hint="@string/input_hint" | ||
android:text="@string/default_url"/> | ||
|
||
<Button | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
/> | ||
android:layout_height="wrap_content" | ||
android:id="@+id/entry_button" | ||
android:text="@string/button_text"/> | ||
|
||
</android.support.constraint.ConstraintLayout> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/layout" | ||
tools:context=".MainActivity"> | ||
|
||
<WebView | ||
android:id="@+id/webview" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
/> | ||
|
||
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<resources> | ||
<string name="app_name">My Application</string> | ||
<string name="app_name">视频webview嵌入测试</string> | ||
<string name="default_url">http://mudu.tv/watch/2758917</string> | ||
<string name="button_text">点击进入</string> | ||
<string name="input_hint">视频地址</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 安卓webview视频播放全屏处理示例 | ||
|
||
|
||
## AndroidManifest设置 | ||
|
||
``` | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.harry.myapplication"> | ||
<!--需要允许网络--> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<!--android9开始usesCleartextTraffic默认为false, 需要设置true--> | ||
<application | ||
android:usesCleartextTraffic="true" | ||
android:allowBackup="true" | ||
android:hardwareAccelerated="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<!--webview所在activity需要设置configChanges--> | ||
<activity | ||
android:name=".MuduRoom" | ||
android:configChanges="orientation|screenSize|keyboardHidden"> | ||
</activity> | ||
</application> | ||
</manifest> | ||
``` | ||
|
||
## activity处理 | ||
|
||
对activity主要进行了一下处理 | ||
|
||
- 1. 设置webViewChromeClient | ||
- 2. 实现webViewChromeClient.onShowCustomView方法,该方法是webview全屏时被调用的方法, 方法具体内容参考代码 | ||
- 3. 实现webViewChromeClient.onShowCustomView方法,该方法是webview退出全屏时被调用的方法,方法具体内容参考代码 | ||
- 4. 设置webViewClient防止使用浏览器打开 |