Android 4.2.2设备上的HTML5视频

时间:2014-05-19 09:34:48

标签: android html5 video

您好我正在尝试构建一个简单的Web视图。

但我无法让视频发挥作用。当我使用4.2.2运行的模拟设备但在实际设备上(Geniatech ATV1220)时,它正在工作我只得到一个灰色的视频框,我根本无法播放视频。它正在加载一秒钟,然后停止。

这是我的主要活动:

public class MainActivity extends Activity {

public static final String PAGE_URL = "http://www.videojs.com";
final Activity activity = this;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);     
    WebView webview = (WebView) findViewById(R.id.webView);

    webview.getSettings().setJavaScriptEnabled(true);   
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setLoadWithOverviewMode(true);

    webview.setWebViewClient(new WebViewClient(){});        
    webview.setWebChromeClient(new WebChromeClient(){
        public void onProgressChanged(WebView view, int progress){
            activity.setTitle("Loading...");
            if(progress == 100){
                activity.setTitle(R.string.app_name);
            }
        }
    });     
    webview.loadUrl(PAGE_URL);
}
}

启用硬件加速的我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidwebview"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    <activity
        android:name="com.example.androidwebview.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

有人可以帮我播放视频吗?我真的不确定为什么会这样。

1 个答案:

答案 0 :(得分:1)

试试这个,我已经检查过了。工作正常。

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);

    mWebChromeClient = new MyWebChromeClient();
    setWebChromeClient(mWebChromeClient);

    setWebViewClient(new MyWebViewClient());

    // 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);

    // 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() {

        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);

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

    @Override
    public Bitmap getDefaultVideoPoster() {
        //Log.i(LOGTAG, "here in on getDefaultVideoPoster");    
        if (mDefaultVideoPoster == null) {
            mDefaultVideoPoster = BitmapFactory.decodeResource(
                    getResources(), R.drawable.default_video_poster);
        }
        return mDefaultVideoPoster;
    }

    @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);
     }
}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(LOGTAG, "shouldOverrideUrlLoading: "+url);
        // don't override URL so that stuff within iframe can work properly
        // view.loadUrl(url);
        return false;
    }
}

static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =


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

家长致电..

public class TestHTML5WebView 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://www.videojs.com/");                
        }

        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();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
    }
}