从资产加载视频转换成HTML5的WebView - 机器人 [英] Load video from assets into HTML5 webview - Android

查看:119
本文介绍了从资产加载视频转换成HTML5的WebView - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题所说,我需要加载从资产的文件夹的视频(.MP4)到web视图。 我试了几件事情:

Like the title says, I need to load a video (.mp4) from the assets folder into a WebView. I've tried a couple of things:

//首先

String webpage = "<!doctype html><head><meta charset='utf-8'></head><body><video id='video' controls='controls' autoplay='autoplay' height=" + m.getHeight() + " width=" + m.getWidth() + "><source src='file:///android_asset/1003/landscape_1003_1_m864.mp4' /></video></body></html>";

wv.loadData(webpage, "text/html", "utf-8");

//二

加载相同的网页,但通过使用loadURL()方法

Load the same webpage but using the loadUrl() method

wv.loadUrl(webpage);

//三

加载完全相同的网页,但与其他方法

Loading the exact same webpage but with another method

wv.loadDataWithBaseURL("file:///android_asset/", webpage,"text/html", "utf-8", null);

我试着改变的第一个参数为空,也该网页,...

I've tried to change the first parameter to null,also the webpage, ...

一切都必须动态地添加(web视图本身的URL的视频,宽度,高度,...),所以作出一个XML文件是不是一种选择。

Everything has to be added dynamically (the webview itself, the url to the video, the width, height, ...) so making a XML file is not an option.

很多,我在互联网上找到的解决方案是将视频的原始文件夹或SD卡,但是这不是一个选项(业务决策)。

A lot of solutions I've found on the internet was to move the video to the Raw folder or SD card, but that is not an option (business decision).

这将是很好,如果有人能帮助我。

It would be nice if somebody could help me.

P.S。如果你能帮助我同样的事情,但显示用类似这样的HTML5视频元素的所有控件(播放,暂停,搜索栏,全屏)随意!在videoview视频

P.S. If you could help me with the exact same thing but showing the video in a videoview with all the controls like a html5 video element (play, pause, seekbar, fullscreen) feel free!

推荐答案

我有同样的问题。不幸的是我没有找到一个解决方案。这Instand我只是做这样的事情:

I had the same issue. Unfortunately I didn't find a solution. Instand of this I just do something like this:

  • 打开网站的WebView
  • 当用户打开该结束的MP4我打开新的活动与视频播放器的链接。
  • 传递网址
  • 打开视频

  • Open the website in webview
  • When user opens the link which ends .mp4 I open new activity with video player.
  • Passing url
  • Open video

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);

parent = (TabLayout)this.getParent();
        setContentView(R.layout.XXXXXXXXXX);
        l = (ProgressBar)findViewById(R.id.webL);
        Resources res = getResources();
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);                
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.loadUrl(res.getString(R.string.XXXXXX));   
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.setWebViewClient(new WebViewClient() 
        {
            @Override
            public void onPageFinished(WebView view, String url)  
            {
                super.onPageFinished(view, url);
                l.setVisibility(View.INVISIBLE);                        
            }

            @Override
            public void onPageStarted (WebView view, String url, Bitmap favicon)
            {
                super.onPageStarted(view, url, favicon);
                l.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                if (url.endsWith(".mp4") )
                {                           
                    data.url = url;
                    Intent intent = new Intent(getApplicationContext(), VideoPlayer.class);
                    startActivity(intent);
                    return true;
                } 
                else 
                {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }

        });

}

..和我的视频播放器:

..and my video player:

public class VideoPlayer extends Activity
{
private VideoView mVideoView;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.video);
    mVideoView = (VideoView) findViewById(R.id.surface_video); 

    mVideoView.setVideoURI(Uri.parse(data.url));
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.requestFocus();
    mVideoView.start();
}

这篇关于从资产加载视频转换成HTML5的WebView - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆