THIS MAY SAVE SOME READING TIME....UPON ATTEMPTING TO PLAY MP4 VIDEO IN MY WEBVIEW I DO NOT HAVE A REQUIREMENT TO PLAY THE VIDEO WITHIN THE WEBVIEW BROWSER. I AM TOTALLY OK WITH SOME CODE THAT KICKS OFF THE NATIVE VIDEO PLAYER FOR ANDROID AS LONG AS THAT WORKS. IF THERE IS A BETTER SOLUTION THAN WHAT I FOUND ON HERE AS FAR AS MODIFYING MY WEBVIEW I'M ALL FOR IT.
First and foremost I am not a Android or JAVA developer. However, providing code and pointing me in the right direction usually helps me get there. Over the last 4 years I have built a site that has a substantial membership. www.GoLiVeFitness.com and www.GoLiveFitness.com/Mobile. The solution is an Asp.Net solution. It basically is a website that allows users to easily search for over 4,800 free exercise video. We built the mobile version of the app which a much scaled down version but much cleaner. Rather than continue to tell users to point their Smartphone browser to the mobile website I decided I would use the Android WebView to load the website into the built-in html5 browser (if I'm not saying this correctly please forgive me).
I completed the webview and it loads the site perfectly. I was even able to deploy it on my Droid Charge and the site loads through the webview perfectly. HERE IS THE PROBLEM....The dynamic video that is being queried and brought back by all types of T-SQL including stored procedures will not play in the webview. When you click the play button it looks like it might start but it never does. I very quickly decided I don't have enough code in my webview to handle something like that. Again, hard-coding a few videos won't work. This site has over 4,800 videos. A user can type legs for instance and get back 130 leg exercises. They can then select one by touching a name link and play the video. ****So my question is how to enable the WebView browser to play the video which is in mp4 format. I saw a solution on here that might work but I'm just not good enough at this to get the code working. What I'm hoping is that someone can help me with modifying the code along with my WebView. When I tried I received many, many, many errors. So I will paste my WebView code first that does run in the WebView browser and then paste the solution code in hopes that someone can tell me what I should change to make it work. Caution: If you can help I will most likely need you to truly show me the correct code. Again, I am very new to this.
-----My code from my WebView code from my GoLiveFitnessActivity.java file---- package golivefitness.mob;
import android.app.Activity;
import android.drm.DrmManagerClient.OnErrorListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.VideoView;
public class GoLiveFitnessActivity extends Activity {
private WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.golivefitness.com/mobile");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Solution I copied from Stack overflow that may solve my problem How to play a video in a webview with android?
public class InredisChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener {
private InterfazWebInredis interfazWeb; // Use Your WebView instance instead
private VideoView mCustomVideoView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private LinearLayout mErrorConsoleContainer;
static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER);
public InredisChromeClient(InterfazWebInredis iwi) {
super();
this.interfazWeb = iwi;
}
public void onShowCustomView(View view, CustomViewCallback callback) {
// super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
mCustomViewContainer = (FrameLayout) view;
mCustomViewCallback = callback;
mContentView = (LinearLayout) interfazWeb.findViewById(R.id.mainContainer);
if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
mCustomVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
// frame.removeView(video);
mContentView.setVisibility(View.GONE);
mCustomViewContainer.setVisibility(View.VISIBLE);
interfazWeb.setContentView(mCustomViewContainer);
mCustomVideoView.setOnCompletionListener(this);
mCustomVideoView.setOnErrorListener(this);
mCustomVideoView.start();
}
}
}
public void onHideCustomView() {
if (mCustomVideoView == null)
return;
// Hide the custom view.
mCustomVideoView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomVideoView);
mCustomVideoView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mCustomViewContainer.setVisibility(View.GONE);
onHideCustomView();
interfazWeb.setContentView(mContentView);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
interfazWeb.setContentView(R.layout.main);
return true;
}
}