5

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

<uses-permission android:name="android.permission.INTERNET"/>

with this code:

    WebView mWebView;
    mWebView = (WebView) findViewById(R.id.ueber_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("file:///android_asset/MyTestPage.htm");

The html page is loading *well*, also the are displayed the video with its border and the typical Youtube-Play-Button in the center of the video. But when I press it, the video doesn't *load*.

This is the htm content:

    <div id="playerFrame">
<iframe src="http://www.youtube.com/embed/MyURL" //have to hide the url, ends with something like "...j4Fs?rel=0"
frameborder="0" allowfullscreen id="playerPlaceHolder"></iframe>
</div>

The css content must be correct cause it's displayed correctly, that shouldn't be the problem.

Any help?

10ff
  • 813
  • 4
  • 16
  • 31

2 Answers2

10

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;
    }

}
10ff
  • 813
  • 4
  • 16
  • 31
  • What is MyOwnWebViewClient? I am trying the in same way but not getting video to play, got only black screen. But my htm file working well in browser. I am using emulator 2.2 and 2.3. Any help will be appreciated. Thanks. – The iCoder Dec 26 '11 at 06:05
  • I added the MyOwnWebViewClient class. You can just copy it. But look at the whole code, I edited it! Now it works perfectly. – 10ff Dec 29 '11 at 09:13
  • Thanks for reply I tried as same as above as you mentioned but its not working , showing only black screen on click of video . – The iCoder Dec 30 '11 at 06:12
  • so the black screen is showing without doing anything or after the click? And before the click, there's the youtube play button with the thumbnail? Or what exactly is happening? – 10ff Jan 02 '12 at 22:16
  • Black screen appears when I click play symbol then it buffer's and black screen appears. – The iCoder Jan 03 '12 at 04:47
  • So the youtube-player controls are loaded? but without a thumbnail. In your html-file, did you use the new Youtube-embed-code? For example: – 10ff Jan 08 '12 at 15:56
  • I tried that also but its not working for me. Tried both way using object tag and also iframe tag . – The iCoder Jan 09 '12 at 05:50
0

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.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154