webview app doesn’t work perfect with Youtube

Asked

Viewed 119 times

0

Create a webview app from my site, on the PC it works perfectly www.pipocaplayfm.com now on the webview it does not play the videos as on the site - App link https://drive.google.com/file/d/0B3xCH5qpmt1vVHl1enhkSVJrYjQ/view?usp=drivesdk

Is there any solution I can apply to it to work properly ?

my code :

' package com.creator.music;

import android.os.Bundle; import android.annotation.Suppresslint; import android.app.Activity; import android.app.Alertdialog; import android.content.Context; import android.content.Dialoginterface; import android.graphics.Bitmap; import android.net.Connectivitymanager; import android.net.Networkinfo; import android.os.Handler; import android.view.View; import android.view.Window; import android.view.Animation.Translateanimation; import android.webkit.Websettings; import android.webkit.Websettings.Renderpriority; import android.webkit.Webview; import android.webkit.Webviewclient; import android.widget.Progressbar; import android.widget.Relativelayout; import android.widget.Toast;

public class Mainactivity extends Activity {

// set your custom url here
String url = " http://bemusic.vebto.com";
boolean doubleBackToExitPressedOnce = false;

// if you want to show progress bar on splash screen
Boolean showProgressOnSplashScreen = true;

WebView mWebView;
ProgressBar prgs;
RelativeLayout splash, main_layout;


@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    mWebView = (WebView) findViewById(R.id.wv);
    prgs = (ProgressBar) findViewById(R.id.progressBar);
    main_layout = (RelativeLayout) findViewById(R.id.main_layout);

    // splash screen View

    if (!showProgressOnSplashScreen)
        ((ProgressBar) findViewById(R.id.progressBarSplash)).setVisibility(View.GONE);
    splash = (RelativeLayout) findViewById(R.id.splash);

// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // // // get status bar height to push webview Below that // int result = 0; // int resourceId = getResources(). getIdentifier("status_bar_height", // "dimen", "android"); // if (resourceId > 0) { // result = getResources(). getDimensionPixelSize(resourceId); // } // // // set top padding to status bar // main_layout.setPadding(0, result, 0, 0); // }

    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {

        mWebView.loadUrl(url);
        Toast.makeText(this,"Recommended to use 3G/4G or Wifi ",Toast.LENGTH_LONG).show();
        // control javaScript and add html5 features
        mWebView.setFocusable(true);
        mWebView.setFocusableInTouchMode(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setDatabasePath(
                this.getFilesDir().getPath() + this.getPackageName()
                        + "/databases/");

        // this force use chromeWebClient
        mWebView.getSettings().setSupportMultipleWindows(true);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (prgs.getVisibility() == View.GONE) {
                    prgs.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                if (prgs.getVisibility() == View.VISIBLE)
                    prgs.setVisibility(View.GONE);

                // check if splash is still there, get it away!
                if (splash.getVisibility() == View.VISIBLE)
                    splash.setVisibility(View.GONE);
                slideToBottom(splash);

            }

        });

    }
    else{

        //Toast.makeText(this,"Your Device is not connected to Internet , Please Turn ON Data Services",Toast.LENGTH_LONG).show();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error");
        builder.setMessage("No Network Connection").setCancelable(false)
                .setIcon(R.drawable.logo)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

        Toast.makeText(this,"Your Device is not connected to Internet , Recommended to use 3G/4G or Wifi ",Toast.LENGTH_LONG).show();

    }

}




/**
 * To animate view slide out from top to bottom
 *
 *
 */
void slideToBottom(View view) {
    TranslateAnimation animate = new TranslateAnimation(0, 0, 0,
            view.getHeight());
    animate.setDuration(2000);
    animate.setFillAfter(true);
    view.startAnimation(animate);
    view.setVisibility(View.GONE);
}

/*@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}*/
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}

}'

1 answer

0


If you are using Android Studio, do the following in your java file, in the webview creation part:

    Toast.makeText(this,"Recommended to use 3G/4G or Wifi ",Toast.LENGTH_LONG).show();
    // control javaScript and add html5 features
    mWebView.setFocusable(true);
    mWebView.setFocusableInTouchMode(true);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.getSettings().setDatabaseEnabled(true);
    mWebView.getSettings().setDatabasePath(
            this.getFilesDir().getPath() + this.getPackageName()
                    + "/databases/");

    // this force use chromeWebClient
    mWebView.getSettings().setSupportMultipleWindows(true);

    mWebView.setWebChromeClient(new WebChromeClient()); //esta linha foi adicionada

    mWebView.loadUrl(url); //esta linha foi movida de lugar

Don’t forget to import:

import android.webkit.WebChromeClient;
  • you refer in Mainactivity.java ? mounted it yes in Android Studio. I’ll add here and see if resolve.

  • Yes, I mean Mainactivity.

  • I’ll add the code to the question, I have no idea where to add it.

  • I indicated which line was added. Another was moved.

  • let me understand in parts: 1 - I copy the code as you passed 2- the code looks like this in the amended part : ' // this force use chromeWebClient mWebView.getSettings(). setSupportMultipleWindows(true);

 mWebView.setWebChromeClient(new WebChromeClient() {}); //esta linha foi adicionada

 mWebView.loadUrl(url); //esta linha foi movida de lugar

 mWebView.setWebViewClient(new WebViewClient() { '

  • Just compare the code that I put with yours. I just tweaked two lines and commented on them.

  • I realized - But I get this error : error: cannot find Symbol class Webchromeclient. .... Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the Compiler error output for Details.

  • I changed the code of the line that was added. Try again and don’t forget to import what you need.

  • that part goes below also ? import android.webkit.Webchromeclient;

  • It goes up there, along with the other Imports.

  • unfortunately it didn’t work

  • Didn’t work because of some mistake?

  • No ... It gave TD right, but the video does not work as it should. When I bought this app the guy said it was not webview, because I knew that webview would not work - I had already tested, but when buying and modifying only the URL as he asked, nothing works. The most invoked is that his demo app works with the site he uses as demo.

  • Soon more I open my android studio here and find the solution for you. I will comment here again after editing the reply.

  • I found out that if I hit the play button twice it will play the video, and now ? how to solve this ?

  • Then you will have to create another question. Because if the video is working, the code is correct. It may be some configuration in the autoplay of Html5.

  • I’m raising here already and you can give a north thank you

  • Put this answer as acceptable. I will help you on the other question.

Show 14 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.