Created
September 1, 2015 09:02
-
-
Save bespechnost/5a5b9668d0061df42911 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; | |
| 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); | |
| String tvzavr = "http://217.29.54.235/stream/tvzavr/3/23a91cc6-b58e-4567-95ca-84da0a4af88b/mnf.m3u8?usessionid=y91lgNzwRB89RtUAqF9x9N0F+PFqsKz13KUdCV+qnXE=&token=a0d8a3447c51e40e1e4d7387d5d1455d5b2f11edf8d71b0d1e7443196eefa48a40c0f44aef06dfad5949dcc42653c68a&pageurl=null&platform=ntv"; | |
| videoView = new VideoView(this); | |
| //videoView.setVideoURI(Uri.parse(tvzavr)); | |
| 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("OnError:" + i); | |
| return false; | |
| } | |
| }); | |
| videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
| @Override | |
| public void onCompletion(MediaPlayer mediaPlayer) { | |
| javaCallBack("OnCompletion:"); | |
| } | |
| }); | |
| videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { | |
| @Override | |
| public void onPrepared(MediaPlayer mediaPlayer) { | |
| videoView.start(); | |
| //javaCallBack("onPrepared:"); | |
| } | |
| }); | |
| videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() { | |
| @Override | |
| public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) { | |
| javaCallBack("onInfo:"); | |
| return false; | |
| } | |
| }); | |
| webView.loadUrl(//"192.168.6.141:8080/a.html"); | |
| "http://192.168.6.141:8080/desktop/"); | |
| // videoView.bringToFront(); | |
| // videoView.requestFocus(); | |
| } | |
| public void javaCallBack(String jsString) { | |
| final String webUrl = "javascript:"+callBackName+"('"+jsString+"')"; | |
| // Add this to avoid android.view.windowmanager$badtokenexception unable to add window | |
| 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(); | |
| javaCallBack("Hello world"); | |
| } | |
| @JavascriptInterface | |
| public void start() { | |
| videoView.start(); | |
| javaCallBack("Hello world"); | |
| } | |
| @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)); | |
| /* | |
| videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { | |
| @Override | |
| public void onPrepared(MediaPlayer mediaPlayer) { | |
| videoView.start(); | |
| } | |
| }); | |
| */ | |
| } | |
| @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() | |
| { | |
| // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it | |
| webChromeClient.onBackPressed(); | |
| } | |
| 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) { | |
| } | |
| @Override | |
| public void onShowCustomView(View view, CustomViewCallback callback) | |
| { | |
| /* | |
| FrameLayout videoFrame = (FrameLayout) view; | |
| videoFrame.setFocusableInTouchMode(true); | |
| videoFrame.setFocusable(true); | |
| videoFrame.setForegroundGravity(Gravity.CENTER); | |
| //viewContainer = videoFrame; | |
| RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); | |
| videoFrame.setLayoutParams(params); | |
| viewContainer.addView(videoFrame); | |
| isCustomView = true; | |
| webView.bringToFront(); | |
| videoFrame.bringToFront(); | |
| */ | |
| } | |
| @Override | |
| public void onHideCustomView() | |
| { | |
| /* | |
| isCustomView = false; | |
| //viewContainer.removeAllViews(); | |
| //viewContainer.addView(webView); | |
| webView.bringToFront(); | |
| webView.requestFocus(); | |
| //webView.goBack(); | |
| */ | |
| } | |
| 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