如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)


問題描述

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

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

參考解法

方法 1:

Not sure if you are still after this, but you could do something like this before you start the video (my videos are stored in the 'res/raw' folder):

Your VideoView intialising code:

VideoView vd = (VideoView)view.findViewById(R.id.video);
vd.setVideoURI(Uri.parse("android.resource:/PACKAGE_NAME/" + getVideo(context, "videoName")));
vd.start();

Code to get your dynamic video:

public static int getVideo(Context context, String name)
{
    Assert.assertNotNull(context);
    Assert.assertNotNull(name);

    return context.getResources().getIdentifier(name, "raw", context.getPackageName());
}

The above code basically allows you to pass a string name into your setVideoURI call. With the second chunk of code returning the relevent android ID in order to play the video (you can call this anywhere within your code).

Hope this helps.

(by user1380537Raj)

參考文件

  1. How do I play dynamic mp4 video within Android WebView I created (CC BY-SA 3.0/4.0)

#android-webview #Android






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論