Saturday, June 9, 2012

Re: [android-developers] Problem with video in WebView

hy
Deniz is correct, u should create your on custom view of web-view  
let me explain u ,see my code 
implement this code and check it out.



public class TestYoutubeVedio extends Activity 
{

    HtmlvVido mWebView;
   
   
  
  
   

    @Override
    public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
     
        mWebView = new HtmlvVido(this);

        if (savedInstanceState != null) 
        {
        mWebView.restoreState(savedInstanceState);
       
            
        } else
        {    
        String Play = "<html><body bgcolor="+"#000000"+"><iframe width=\"1100px\" height=\"1500px\" src=\"http://www.youtube.com/embed/g6-8zkbCjl8\" frameborder=\"0\" allowfullscreen></iframe></body></html>";      
          
        
        }
      
        setContentView(mWebView.getLayout()); 
          
    }

    @Override
    public void onSaveInstanceState(Bundle outState) 
    {
    
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
       
      
    }

    @Override
    public void onStop() 
    {
   
        super.onStop();
        
        mWebView.stopLoading();
    }
    
}

create ur custom web-view 


public class HtmlvVido extends WebView
{

    private Context                             mContext;
    private MyWebChromeClient                   mWebChromeClient;
    private View                                mCustomView;
    private FrameLayout                         mCustomViewContainer;
    private WebChromeClient.CustomViewCallback  mCustomViewCallback;

    private static FrameLayout                         mContentView;
    private FrameLayout                         mBrowserFrameLayout;
    private FrameLayout                         mLayout;
 
    FrameLayout COVER_SCREEN_PARAMS1;

   
    static final String LOGTAG = "HTML5WebView";

    @SuppressWarnings("deprecation")
private void init(Context context) 
    {
        mContext = context;     
        Activity a = (Activity) mContext;

        mLayout= new FrameLayout(context);
       
 
      
      
    
      

        mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custum, null);
     
        
      
        mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
        mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);

        mLayout.addView(mBrowserFrameLayout,COVER_SCREEN_PARAMS);
       
        

        // Configure the webview
        WebSettings s = getSettings();
        s.setBuiltInZoomControls(true);
        s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        s.setUseWideViewPort(true);
        s.setLoadWithOverviewMode(true);
      //  s.setSavePassword(true);
        s.setSaveFormData(true);
        s.setJavaScriptEnabled(true);
      
        mWebChromeClient = new MyWebChromeClient();
        setWebChromeClient(mWebChromeClient);

        setWebViewClient(new WebViewClient());

setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        // enable navigator.geolocation 
       // s.setGeolocationEnabled(true);
       // s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");

        // enable Web Storage: localStorage, sessionStorage
       // s.setDomStorageEnabled(true);

        mContentView.addView(this);
    }

    public HtmlvVido(Context context) 
    {
        super(context);
        init(context);
    }

    public HtmlvVido(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HtmlvVido(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        
       
        
        init(context);
    }

    public FrameLayout getLayout() 
    {
   
        return mLayout;
    }

    public boolean inCustomView() {
        return (mCustomView != null);
    }

    public void hideCustomView() {
        mWebChromeClient.onHideCustomView();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((mCustomView == null) && canGoBack()){
                goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyWebChromeClient extends WebChromeClient 
    {
        private Bitmap      mDefaultVideoPoster;
        private View        mVideoProgressView;

        @Override
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
        {
            //Log.i(LOGTAG, "here in on ShowCustomView");
            HtmlvVido.this.setVisibility(View.GONE);

            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }

            mCustomViewContainer.addView(view);
            mCustomView = view;
            mCustomViewCallback = callback;
            mCustomViewContainer.setVisibility(View.VISIBLE);
        }
        @Override
        public void onHideCustomView() {
            System.out.println("customview hideeeeeeeeeeeeeeeeeeeeeeeeeee");
            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();

            HtmlvVido.this.setVisibility(View.VISIBLE);
            HtmlvVido.this.goBack();
            //Log.i(LOGTAG, "set it to webVew");
        }


        @Override
        public View getVideoLoadingProgressView() {
            //Log.i(LOGTAG, "here in on getVideoLoadingPregressView");

            if (mVideoProgressView == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                mVideoProgressView = inflater.inflate(R.layout.vedioloadingprogress, null);
            }
            return mVideoProgressView; 
        }

         @Override
         public void onReceivedTitle(WebView view, String title)
         {
            ((Activity) mContext).setTitle(title);
         }

         @Override
         public void onProgressChanged(WebView view, int newProgress)
         {
             ((Activity) mContext).getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
         }

         @Override
         public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
             callback.invoke(origin, true, false);
         }
    }
    


    static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    
}


 
custom.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
        <LinearLayout
 android:orientation="horizontal"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:id="@+id/lener">

 </LinearLayout>


    <FrameLayout android:id="@+id/fullscreen_custom_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  
          </FrameLayout>
    
    <RelativeLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/re"
        >

        <LinearLayout android:id="@+id/error_console"
            android:layout_width="match_parent"
            android:layout_alignBottom="@id/re"
            android:layout_height="wrap_content"
   
   
        >
        
     </LinearLayout>

        <FrameLayout android:id="@+id/main_content"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
        >

        </FrameLayout>
    </RelativeLayout>
</FrameLayout>


On Sun, Jun 10, 2012 at 1:04 AM, Angélica Oliveira <angelica.liv@gmail.com> wrote:
I tried playing .mp4 video, from my internal storage.

I saw something about implement onShowCustomView but I didn't realize what I have to do, can you (Deniz) give me an example?

I already searched it on google, but I didn't find anything, I would like to watch the video on the webview, not in a VideoView...

Thank's!


2012/6/7 Narendra Singh Rathore <nsr.curious@gmail.com>
Are you sure, your device supports swf file, or you have proper plugin/player for it?
May be that's the problem.


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment