Created
September 1, 2015 13:50
-
-
Save bespechnost/ed287cae83747f991135 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.example.vkulibaba.smartnemotv; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.graphics.Color; | |
| import android.media.MediaPlayer; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.webkit.JavascriptInterface; | |
| import android.webkit.WebChromeClient; | |
| import android.webkit.WebView; | |
| import android.widget.RelativeLayout; | |
| import android.widget.VideoView; | |
| import android.view.KeyEvent; | |
| public class MainActivity extends Activity { | |
| private ViewGroup videoFrame; | |
| private WebView webView; | |
| private VideoView videoView; | |
| private JsObject jsObject; | |
| private Activity activity; | |
| private VideoWebChromeClient webChromeClient; | |
| private String callBackName = "androidCallBack"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| activity = this; | |
| videoFrame = new RelativeLayout(this); | |
| videoView = new VideoView(this); | |
| webView = new WebView(this); | |
| webChromeClient = new VideoWebChromeClient(webView, videoFrame); | |
| webView.getSettings().setJavaScriptEnabled(true); | |
| webView.getSettings().setAllowContentAccess(true); | |
| webView.getSettings().setAppCacheEnabled(true); | |
| webView.getSettings().setDomStorageEnabled(true); | |
| webView.setFocusable(true); | |
| webView.setFocusableInTouchMode(true); | |
| WebView.setWebContentsDebuggingEnabled(true); | |
| webView.setBackgroundColor(Color.TRANSPARENT); | |
| webView.setWebChromeClient(webChromeClient); | |
| webView.setBackgroundColor(Color.TRANSPARENT); | |
| webView.requestFocus(); | |
| videoFrame.addView(videoView); | |
| videoFrame.addView(webView); | |
| setContentView(videoFrame); | |
| jsObject = new JsObject(this); | |
| webView.addJavascriptInterface(jsObject, "Android"); | |
| videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { | |
| @Override | |
| public boolean onError(MediaPlayer mediaPlayer, int i, int i1) { | |
| javaCallBack("{'name':'OnError','value':'" + i + "'}"); | |
| return false; | |
| } | |
| }); | |
| videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
| @Override | |
| public void onCompletion(MediaPlayer mediaPlayer) { | |
| javaCallBack("{'name':'OnCompletion','value':'" + 0 + "'}"); | |
| } | |
| }); | |
| videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { | |
| @Override | |
| public void onPrepared(MediaPlayer mediaPlayer) { | |
| videoView.start(); | |
| javaCallBack("{'name':'onPrepared','value':'" + 0 + "'}"); | |
| } | |
| }); | |
| videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() { | |
| @Override | |
| public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) { | |
| javaCallBack("{'name':'onInfo','value':'" + i + "'}"); | |
| return false; | |
| } | |
| }); | |
| webView.loadUrl("http://192.168.6.141:8080/android/"); | |
| } | |
| public void javaCallBack(String jsString) { | |
| final String webUrl = "javascript:"+callBackName+"("+jsString+")"; | |
| if(!activity.isFinishing()) | |
| // loadurl on UI main thread | |
| activity.runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| webView.loadUrl(webUrl); | |
| } | |
| }); | |
| } | |
| private class JsObject { | |
| private Context context; | |
| public JsObject(Context context) { | |
| this.context = context; | |
| } | |
| @JavascriptInterface | |
| public void setCallBack(String name) { | |
| callBackName = name; | |
| } | |
| @JavascriptInterface | |
| public void suspend() { | |
| videoView.suspend(); | |
| } | |
| @JavascriptInterface | |
| public void start() { | |
| videoView.start(); | |
| } | |
| @JavascriptInterface | |
| public void stopPlayback() { | |
| videoView.stopPlayback(); | |
| } | |
| @JavascriptInterface | |
| public void setVideoURI(String videoUri) { | |
| videoView.setVideoURI(Uri.parse(videoUri)); | |
| } | |
| @JavascriptInterface | |
| public void play(String videoUri) { | |
| if (videoView.isPlaying()) { | |
| videoView.stopPlayback(); | |
| } | |
| videoView.setVideoURI(Uri.parse(videoUri)); | |
| } | |
| @JavascriptInterface | |
| public boolean pause() { | |
| if (videoView.canPause()) { | |
| videoView.pause(); | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| @JavascriptInterface | |
| public void resume() { | |
| videoView.resume(); | |
| } | |
| @JavascriptInterface | |
| public int getDuration() { | |
| return videoView.getDuration(); | |
| } | |
| @JavascriptInterface | |
| public int getCurrentPosition() { | |
| return videoView.getCurrentPosition(); | |
| } | |
| @JavascriptInterface | |
| public void seekTo(int mSec) { | |
| videoView.seekTo(mSec); | |
| } | |
| @JavascriptInterface | |
| public boolean isPlaying() { | |
| return videoView.isPlaying(); | |
| } | |
| } | |
| @Override | |
| public void onBackPressed() | |
| { | |
| webView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ESCAPE)); | |
| } | |
| public class VideoWebChromeClient extends WebChromeClient { | |
| private WebView webView; | |
| private ViewGroup viewContainer; | |
| private boolean isCustomView = false; | |
| public VideoWebChromeClient(WebView webView, ViewGroup viewContainer) { | |
| this.webView = webView; | |
| this.viewContainer = viewContainer; | |
| } | |
| @Override @SuppressWarnings("deprecation") | |
| public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) // Available in API level 14+, deprecated in API level 18+ | |
| { | |
| onShowCustomView(view, callback); | |
| } | |
| @Override | |
| public void onProgressChanged(WebView view, int newProgress) { | |
| } | |
| public boolean onBackPressed() | |
| { | |
| if (isCustomView) | |
| { | |
| onHideCustomView(); | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment