Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to play video is gotten from the webpage in WebViewJavaScriptInterface? #451

Closed
nickcarterney opened this issue May 12, 2017 · 2 comments

Comments

@nickcarterney
Copy link

nickcarterney commented May 12, 2017

Hi Everyone,

I getting issue: load uri videos from the website and play it. In the first time, exoPlayer works well in the MainActivity class. If move the code into WebViewJavaScriptInterface to start Video with url is gotten from the webpage, It will not work :(

Helps me!

ExoMedia version: 4.0.0-preview
Device OS version: 5.1
Device Name: SCL 23 (SAMSUNG GALAXY S5)

To start Video, please click on the white screen area after the webpage is loaded
In the MainActivity class:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        mVisible = true;
        mControlsView = findViewById(R.id.fullscreen_content_controls);
        mContentView = findViewById(R.id.fullscreen_content);

        webview = (WebView) findViewById(R.id.webviewid);
        videoView = (VideoView)findViewById(R.id.video_view);
        videoView.setVisibility(View.INVISIBLE);

        /* add the following line ---------- */
        final WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        webview.setWebViewClient(new WebViewClient() {
            // autoplay when finished loading via javascript injection
            public void onPageFinished(WebView view, String url) {
                webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
            }

        });
        webview.setWebChromeClient(new WebChromeClient());
        webview.addJavascriptInterface(new WebViewJavaScriptInterface(videoView, this), JS_INTERFACE);
        webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webview.getSettings().setJavaScriptEnabled(true);
        //webview.clearCache(true);
        WebBackForwardList lst_history = webview.copyBackForwardList();
        String link = "http://beeb.vn";
        if(lst_history.getSize() >= 1)
            link = lst_history.getItemAtIndex(lst_history.getCurrentIndex() - 1).getUrl();

        String versionName = BuildConfig.VERSION_NAME;
        webview.getSettings().setUserAgentString("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) BeebVN/"+versionName+" in Android OS by beeb.vn");

        webview.loadUrl(link);
    }
    @Override
    public void onBackPressed() {
        if(videoView.getVisibility() == View.VISIBLE)
        {
            videoView.stopPlayback();
            videoView.setVisibility(View.INVISIBLE);
            return;
        }
}

In the WebViewJavaScriptInterface class:

public class WebViewJavaScriptInterface
{
    private VideoView videoView;
    private MainActivity activity;
    public WebViewJavaScriptInterface(VideoView _videoView, MainActivity _activity){
        videoView = _videoView;
        activity = _activity;
    }
    @JavascriptInterface
    public void showAdBanner(String uriHLS) {
//    webView.loadUrl("javascript:window.Android.showAdBanner(showSdkAd())");
        //For now we just picked an arbitrary item to play
        videoView = (VideoView)activity.findViewById(R.id.video_view);
        videoView.setVisibility(View.VISIBLE);
        videoView.setVideoURI(Uri.parse(uriHLS));
//        videoView.setOnPreparedListener(activity);
//        videoView.setOnErrorListener(activity);
        videoView.start();
    }
}
@brianwernick
Copy link
Owner

brianwernick commented May 12, 2017

This is more of a www.stackoverflow.com question and not something specific to ExoMedia.

@nickcarterney
Copy link
Author

hmm.... I Need Support. And I think maybe it's a bug :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants