Youtube Video in WebView doesn't load

后端 未结 2 766
独厮守ぢ
独厮守ぢ 2020-12-03 23:19

I\'ve implemented a WebView in my Activity, for that I\'ve given the permission


         


        
相关标签:
2条回答
  • 2020-12-03 23:47

    I was facing same problem I have shared my code on github. All you need to do is call this line of code :-

            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.frag_root,
                            UniversalWebViewFragment.newInstance(
                                    <place your you tube url>, false)).commit();
    

    It support portrait mode as well.

    0 讨论(0)
  • 2020-12-03 23:53

    Now it works, with a combination of some things:

    public class Videos extends Activity {
    
    private WebView mWebView;
    private String extra;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videos_layout);       
    
        extra = getIntent().getStringExtra("VideosId");        
        mWebView = (WebView) findViewById(R.id.videos_webview); 
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
    
        final Activity activity = this;
        mWebView.setWebChromeClient(new WebChromeClient() {
          public void onProgressChanged(WebView view, int progress) {
            // Activities and WebViews measure progress with different scales.
            // The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 1000);
          }
        });
    
        mWebView.setWebViewClient(new MyOwnWebViewClient());   
    
    
        mWebView.loadUrl("file:///android_asset/Videos/"+extra+".htm");     
        mWebView.setWebViewClient(new MyOwnWebViewClient());      
    }
    
    //Ermöglicht das Zurücknavigieren im Browser mittels Back-Button
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    And these entries in the manifest:

        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:hardwareAccelerated = "true">
    

    But I don't know, which one are at least necessary. Further, it's not possible to skip parts of the video, I have to watch it in one turn. Anyone knows a solution?

    And MyOwnWebViewClient:

    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    //Enables the possibility for the User to navigate in the WebView and prevents,
    //that the Standard-Browser is invoked
    
    public class MyOwnWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题