-1

I have an tabbed application realized with ABS and Fragments. One of this fragments should contain a webview that shows a certain youtube channel, where the user can look through the channel's videos and watch them, if he likes. Therefore I want to show the videos in the webview. Everything works fine until I click play to watch a video. Than it only shows the loading circle. I've tried some approaches I found here, like turning on hardware acceleration or implemeting my own WebChromeClient. Either it changes nothing or the application offers me to select the native browser or Chrome to open the link. Any idea what I have to do get the videos to play inside the webview? Attached is my current implementation:

private WebView wv; 
private FrameLayout mContentView;  
private FrameLayout mCustomViewContainer; 
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;  
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    setHasOptionsMenu(true);
    View v = inflater.inflate(R.layout.didacta_channel_layout, container, false);
    wv = (WebView) v.findViewById(R.id.test_web);
    mContentView = (FrameLayout) v.findViewById(R.id.main_content);
    mCustomViewContainer = (FrameLayout) v.findViewById(R.id.fullscreen_custom_content);
    wv.getSettings().setJavaScriptEnabled(true);
    if (Build.VERSION.SDK_INT < 8) {
        wv.getSettings().setPluginsEnabled(true);
    } else {
        wv.getSettings().setPluginState(PluginState.ON);
    }
    wv.setWebChromeClient(new DDChromeClient());
    wv.loadUrl("http://www.youtube.com/user/bikltv");

    return v;
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);

    menu.add(0, 1, 0, "Restart")
    .setIcon(R.drawable.ic_menu_refresh)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if(item.getItemId() == android.R.id.home)
    {
        ((DidactaNavigationActivity)getActivity()).finish();
    }
    if(item.getItemId() == 1)
    {
        wv.loadUrl("http://www.youtube.com/user/bikltv");
    }
    return super.onOptionsItemSelected(item);
}
class DDChromeClient extends WebChromeClient
{
    public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
    {
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null)
        {
            callback.onCustomViewHidden();
            return;
        }

        // Add the custom view to its container.
        mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
        mCustomView = view;
        mCustomViewCallback = callback;

        // hide main browser view
        mContentView.setVisibility(View.GONE);

        // Finally show the custom view container.
        mCustomViewContainer.setVisibility(View.VISIBLE);
        mCustomViewContainer.bringToFront();
    }

    public void onHideCustomView()
    {
        if (mCustomView == null)
            return;

        // Hide the custom view.
        mCustomView.setVisibility(View.GONE);
        // Remove the custom view from its container.
        mCustomViewContainer.removeView(mCustomView);
        mCustomView = null;
        mCustomViewContainer.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();

        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Tobi N
  • 554
  • 6
  • 22
  • android webview is buggy better use youtube api to load 3gp video and play in native videoview – Biraj Zalavadia Sep 11 '13 at 11:28
  • How can I do this when I want to show a channel and not only a single video? – Tobi N Sep 11 '13 at 11:32
  • Possible duplicate of [Youtube Video in WebView doesn't load](https://stackoverflow.com/questions/8475707/youtube-video-in-webview-doesnt-load) – Zoe Apr 30 '19 at 17:51

2 Answers2

0

Dont use a WebView to play YouTube Videos? It won't be needed if you use the Android YouTube API. More information in Click Here .

With the Android YouTube API,you can Pass VID to the YouTube Android Player that's it. The video will get Played in your Application

String
  • 3,660
  • 10
  • 43
  • 66
0

Finally foun my solution here... https://stackoverflow.com/a/8490751/687475

Setting the webview client two times after setting the webchrome client did it somehow. For me the easier solution for showing a complete channel.

Community
  • 1
  • 1
Tobi N
  • 554
  • 6
  • 22