6

I am succeeded to play streamed Youtube video from HTML5 content in Webview in Android but now problem is that video plays only first time. After that VideoView only goes to end of the video file.

I tried clearing cache as suggested here but no luck.

What could be the possible solution for this problem?

Please try following code to run Video, this has been using some suggestions given on stackoverflow.com

WebView webView = (WebView) findViewById(R.id.product_details_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebChromeClient(new chromeClient());
webView.setWebViewClient(new WebViewClient(){

});
webView.loadUrl(url);

public class chromeClient extends WebChromeClient implements OnCompletionListener,  
OnErrorListener{
    private WebView wv;
    private VideoView mVideoView;
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new   
FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        if (view instanceof FrameLayout) {
            wv = (WebView)findViewById(R.id.product_details_webview);
            mCustomViewContainer = (FrameLayout) view;
            mCustomViewCallback = callback;
            mContentView = (LinearLayout)findViewById(R.id.linearlayout1);
            if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
                mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
                // frame.removeView(video);
                mContentView.setVisibility(View.GONE);
                mCustomViewContainer.setVisibility(View.VISIBLE);
                setContentView(mCustomViewContainer);
                mVideoView.setOnCompletionListener(this);
                mVideoView.setOnErrorListener(this);
                mVideoView.start();

            }
        }
    }

    public void onHideCustomView() {
        if (mVideoView == null){
            return;
        }else{
        // Hide the custom view.
        mVideoView.setVisibility(View.GONE);
        // Remove the custom view from its container.
        mCustomViewContainer.removeView(mVideoView);
        mVideoView = null;
        mCustomViewContainer.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();
        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
        }
    }


    public void onCompletion(MediaPlayer mp) {
        mp.stop();
        mCustomViewContainer.setVisibility(View.GONE);
        onHideCustomView();
        setContentView(mContentView);
    }

    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
        setContentView(mContentView);
        return true;
    }
  }
Community
  • 1
  • 1
silwar
  • 6,470
  • 3
  • 46
  • 66

3 Answers3

4

Add in your chrome client :

        @Override public void onPrepared(MediaPlayer mp) {

        customViewCallback.onCustomViewHidden();
    }

where customViewCallback = callback;

letroll
  • 1,059
  • 1
  • 14
  • 36
  • i added same thing im getting crash please tell me how to get rid of that crash? check the link for crash http://stackoverflow.com/questions/14229077/you-tube-playing-only-once – Manju Jan 09 '13 at 06:14
1

You got a video working inside a WebView with HTML5? Congrats! I failed trying to do that :(

To fix my issue I ended up leaving a place holder in the HTML and handling the video from native code using the MediaPlayer class.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • can you tell me is there directly play mp4 file in android browser because i cant able to play. i have record file(mp4 formate file) with mediarecoder class and store in server but not get success to play at browser – Nikunj Patel Nov 29 '11 at 12:41
  • @Dr.nik: Sorry. I don't understand your question :( – Macarse Nov 29 '11 at 12:42
  • @Dr.nik: What I said in my answer is that I had no luck playing a video from a `WebView` so I handle it from the native code (java) using the `MediaPlayer` class which comes with the SDK. – Macarse Nov 29 '11 at 12:49
  • Hi I am not able to post my code (Which I have used to play streamed video in webview) in the comment. Please let me know if there any other way to sent it to you. Hope it will solve your problem – silwar Nov 30 '11 at 12:50
  • @silwar: Edit your question and place it there. – Macarse Nov 30 '11 at 12:59
  • @PinkCandy please try this ... add "android:hardwareAccelerated="true" in corresponding activity tag of Manifest file – silwar Feb 10 '12 at 08:57
0

I had this problem as well. I had to add a seek(0). Seems like the next video played from exactly where the last video ended. So I had to go to the beginning of the video before playing.

Leo
  • 1,495
  • 23
  • 41