html webview视频播放,WebView和HTML 5<视频>

经过长时间的研究,我让这个东西正常工作了。请参阅下列代码:

Test.javaimport android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;public class Test extends Activity {

HTML5WebView mWebView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mWebView = new HTML5WebView(this);

if (savedInstanceState != null) {

mWebView.restoreState(savedInstanceState);

} else {

mWebView.loadUrl("http://192.168.1.18/xxxxxxxxxxxxxxxx/");

}

setContentView(mWebView.getLayout());

}

@Override

public void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

mWebView.saveState(outState);

}

@Override

public void onStop() {

super.onStop();

mWebView.stopLoading();

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

if (mWebView.inCustomView()) {

mWebView.hideCustomView();

//  mWebView.goBack();

//mWebView.goBack();

return true;

}

}

return super.onKeyDown(keyCode, event);

}}

HTML%VIDEO.javapackage com.ivz.idemandtest;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.

BitmapFactory;import android.util.AttributeSet;import android.util.Log;import android.view.KeyEvent;import android.view.LayoutInflater;

import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.webkit.GeolocationPermissions;

import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;

import android.widget.FrameLayout;public class HTML5WebView extends WebView {

private Context                             mContext;

private MyWebChromeClient                   mWebChromeClient;

private View                                mCustomView;

private FrameLayout                         mCustomViewContainer;

private WebChromeClient.CustomViewCallback  mCustomViewCallback;

private FrameLayout                         mContentView;

private FrameLayout                         mBrowserFrameLayout;

private FrameLayout                         mLayout;

static final String LOGTAG = "HTML5WebView";

private void init(Context context) {

mContext = context;

Activity a = (Activity) mContext;

mLayout = new FrameLayout(context);

mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custom_screen, null);

mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);

mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);

mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);

// Configure the webview

WebSettings s = getSettings();

s.setBuiltInZoomControls(true);

s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

s.setUseWideViewPort(true);

s.setLoadWithOverviewMode(true);

//  s.setSavePassword(true);

s.setSaveFormData(true);

s.setJavaScriptEnabled(true);

mWebChromeClient = new MyWebChromeClient();

setWebChromeClient(mWebChromeClient);

setWebViewClient(new WebViewClient());setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

// enable navigator.geolocation

// s.setGeolocationEnabled(true);

// s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");

// enable Web Storage: localStorage, sessionStorage

s.setDomStorageEnabled(true);

mContentView.addView(this);

}

public HTML5WebView(Context context) {

super(context);

init(context);

}

public HTML5WebView(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

public HTML5WebView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context);

}

public FrameLayout getLayout() {

return mLayout;

}

public boolean inCustomView() {

return (mCustomView != null);

}

public void hideCustomView() {

mWebChromeClient.onHideCustomView();

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

if ((mCustomView == null) && canGoBack()){

goBack();

return true;

}

}

return super.onKeyDown(keyCode, event);

}

private class MyWebChromeClient extends WebChromeClient {

private Bitmap      mDefaultVideoPoster;

private View        mVideoProgressView;

@Override

public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)

{

//Log.i(LOGTAG, "here in on ShowCustomView");

HTML5WebView.this.setVisibility(View.GONE);

// if a view already exists then immediately terminate the new one

if (mCustomView != null) {

callback.onCustomViewHidden();

return;

}

mCustomViewContainer.addView(view);

mCustomView = view;

mCustomViewCallback = callback;

mCustomViewContainer.setVisibility(View.VISIBLE);

}

@Override

public void onHideCustomView() {

System.out.println("customview hideeeeeeeeeeeeeeeeeeeeeeeeeee");

if (mCustomView == null)

return;

// Hide the custom view.

mCustomView.setVisibility(View.GONE);

// Remove the custom view from its container.

mCustomViewContainer.removeView(mCustomView);

mCustomView = null;

mCustomViewContainer.setVisibility(View.GONE);

mCustomViewCallback.onCustomViewHidden();

HTML5WebView.this.setVisibility(View.VISIBLE);

HTML5WebView.this.goBack();

//Log.i(LOGTAG, "set it to webVew");

}

@Override

public View getVideoLoadingProgressView() {

//Log.i(LOGTAG, "here in on getVideoLoadingPregressView");

if (mVideoProgressView == null) {

LayoutInflater inflater = LayoutInflater.from(mContext);

mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);

}

return mVideoProgressView;

}

@Override

public void onReceivedTitle(WebView view, String title) {

((Activity) mContext).setTitle(title);

}

@Override

public void onProgressChanged(WebView view, int newProgress) {

((Activity) mContext).getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);

}

@Override

public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {

callback.invoke(origin, true, false);

}

}

static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =

new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);}

自定义屏幕文件<?xml  version="1.0" encoding="utf-8"?>

android:visibility="gone"

android:background="@color/black"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

视频加载方案<?xml  version="1.0" encoding="utf-8"?>

android:id="@+id/progress_indicator"

android:orientation="vertical"

android:layout_centerInParent="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

style="?android:attr/progressBarStyleLarge"

android:layout_gravity="center"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="@string/loading_video" android:textSize="14sp"

android:textColor="?android:attr/textColorPrimary" />

color s.xml<?xml  version="1.0" encoding="utf-8"?>

#ffffffff

#ff000000

#ffffffff

#ff000000

#ffffffff

#ffffffff

#ffffffff

#ff000000

#ffdddddd

Manifest.xml<?xml  version="1.0" encoding="utf-8"?>

package="com.test"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

android:configChanges="orientation|keyboardHidden|keyboard">

  

permission.ACCESS_COARSE_LOCATION"/>

android:name="android.permission.ACCESS_MOCK_LOCATION"/>

CCESS_LOCATION" />

期待其他你能理解的事情。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值