简体   繁体   English

如何在我创建的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. 这可能节省了一些阅读时间。...试图在我的Webview中播放MP4视频时,我不需要在Webview浏览器中播放视频。 I AM TOTALLY OK WITH SOME CODE THAT KICKS OFF THE NATIVE VIDEO PLAYER FOR ANDROID AS LONG AS THAT WORKS. 我完全可以接受一些代码,该代码可以使ANDROID的原生视频播放器正常运行。 IF THERE IS A BETTER SOLUTION THAN WHAT I FOUND ON HERE AS FAR AS MODIFYING MY WEBVIEW I'M ALL FOR IT. 如果有比我在修改我的Webview时发现的更好的解决方案,我全力以赴。

First and foremost I am not a Android or JAVA developer. 首先,我不是Android或JAVA开发人员。 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. www.GoLiVeFitness.com和www.GoLiveFitness.com/Mobile。 The solution is an Asp.Net solution. 该解决方案是Asp.Net解决方案。 It basically is a website that allows users to easily search for over 4,800 free exercise video. 它基本上是一个网站,允许用户轻松搜索超过4,800个免费运动视频。 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). 我没有继续告诉用户将智能手机浏览器指向移动网站,而是决定使用Android WebView将网站加载到内置html5浏览器中(如果我不能正确地说出这一点,请原谅我)。

I completed the webview and it loads the site perfectly. 我完成了webview,它完美地加载了网站。 I was even able to deploy it on my Droid Charge and the site loads through the webview perfectly. 我什至可以将其部署在Droid Charge上,并且可以完美地通过Webview加载网站。 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. 问题就在这里...。所有类型的T-SQL(包括存储过程)正在查询并带回的动态视频都不会在Web视图中播放。 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. 我很快决定我的Web视图中没有足够的代码来处理类似的事情。 Again, hard-coding a few videos won't work. 同样,对一些视频进行硬编码将不起作用。 This site has over 4,800 videos. 该网站有超过4,800个视频。 A user can type legs for instance and get back 130 leg exercises. 例如,用户可以键入腿部,并进行130次腿部锻炼。 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. ****所以我的问题是如何使WebView浏览器播放mp4格式的视频。 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. 我希望有人可以帮助我修改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. 因此,我将首先粘贴在WebView浏览器中运行的WebView代码,然后粘贴解决方案代码,以希望有人可以告诉我应该进行哪些更改才能使其正常工作。 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; -----我的代码来自我的GoLiveFitnessActivity.java文件中的WebView代码----软件包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? 我从Stack溢出复制的解决方案可以解决我的问题如何使用android在Webview中播放视频?

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

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): 不确定是否仍在执行此操作,但是在开始播放视频之前,您可以执行以下操作(我的视频存储在“ res / raw”文件夹中):

Your VideoView intialising code: 您的VideoView初始化代码:

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. 上面的代码基本上允许您将字符串名称传递给setVideoURI调用。 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). 第二段代码返回相关的Android ID以播放视频(您可以在代码内的任何位置调用此代码)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM