GithubHelp home page GithubHelp logo

tianhong1989 / videoenabledwebview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cprcrack/videoenabledwebview

0.0 1.0 0.0 114 KB

Android's WebView and WebChromeClient class extensions that enable fully working HTML5 video support

License: MIT License

Java 100.00%

videoenabledwebview's Introduction

VideoEnabledWebView

Android's WebView and WebChromeClient class extensions that enable fully working, cross-device, HTML5 video support in Android 2.2 (API level 8) onwards. Actively maintained and tested up to Android 4.4 (API level 19) with its new Chromium webview.

Motivation

Android's default WebView doesn't work well with HTML5 videos (i.e. the <video> tag). Unlike iOS's UIWebView, which is similar to a Safari tab with respect to video handling, Android's implementation is far from how a Chrome tab behaves. API level fragmentation and manufacturer customizations, which usually include video player related UI changes, only add up to the mess. Things that don't work consistently across devices include:

  • Videos not even playing.
  • Videos not playing in-line.
  • Videos not displaying any status indicator while loading.
  • Videos not going full-screen.
  • Videos not automatically exiting full-screen when they end.
  • Videos not playing for the second time.

VideoEnabledWebView and VideoEnabledWebChromeClient are two handy extension classes that come to help deal with these issues. I originally wrote them for my personal use, but they got a lot of attention in StackOverflow, and that's the reason they are now here. Contributions are appreciated.

How to use it

For a working example, download the whole repository and open it with Android Studio. Do not use the Open Project option, use Import Project.

As you can see in the example project, you first need to include VideoEnabledWebView.java and VideoEnabledWebChromeClient.java classes into your project. Second, you need to carefully read both classes' comments, as they are fully documented with javadoc and include very important information on how to use them correctly based on your needs.

VideoEnabledWebChromeClient can be used alone if you do not require the functionality that VideoEnabledWebView adds, although you need to include both classes anyway in order to compile. On the other side, VideoEnabledWebView must always rely on a VideoEnabledWebChromeClient.

Finally, you need to define all the views that you will be using in your layout files, and provide the relevant references in the classes' constructors.

Common issues check-list

  1. Remember to declare the internet permission in AndroidManifest.xml if you are using the WebView to access remote content: <uses-permission android:name="android.permission.INTERNET" />
  2. Remember to enable hardware acceleration in AndroidManifest.xml for in-line videos to work in API level 11. The field will have no effect in earlier API levels: android:hardwareAccelerated="true"
  3. Remember to initialize the VideoEnabledWebChromeClient and link it with the VideoEnabledWebView. Follow the example in ExampleActivity.java.
  4. Remember to override your Activity's onBackPressed() and pass the event to the VideoEnabledWebChromeClient. Follow the example in ExampleActivity.java.
  5. Remember to specify and/or programmatically inflate the videoLayout, nonVideoLayout, and, optionally, loadingView. Follow the example in ExampleActivity.java and the xml layout files in res/layout.
  6. If you are using ProGuard, remember to add the fully qualified name of the Javascript interface to the rules file: -keepclassmembers class name.cpr.VideoEnabledWebView$JavascriptInterface { public *; }

videoenabledwebview's People

Contributors

cprcrack avatar crandellws avatar rdp avatar takahirom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.