在Android中的WebView播放本地视频 [英] Playing local video in WebView on Android

查看:426
本文介绍了在Android中的WebView播放本地视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个的WebView的应用程序多数民众赞成应该播放视频,这是保存在本地。奇怪的是视频播放器不工作与本地的视频文件。它播放视频保存在服务器上,但。

I'm building an app with a WebView thats supposed to play a video, that's saved locally. Strangely the video player is not working with local video files. It does play videos saved on a server though.

本地文件(html和视频)保存在文件夹中资产/ html_test

The local files (html and video) are saved in a folder assets/html_test

下面是文件。

HTML

<div class="video-container">
  <p>Server</p>
  <video poster="video/star.png" controls>
    <source src="http://broken-links.com/tests/media/BigBuck.m4v" />
  </video>
</div>

<div class="video-container">
  <p>local</p>
  <video poster="video/star.png" controls>
    <source src="BigBuck.m4v" />
  </video>
</div>

的onCreate在活动

WebView browser = (WebView) findViewById(R.id.browser);

WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
webSettings.setAllowFileAccessFromFileURLs(true);

browser.setWebChromeClient(new WebChromeClient());

browser.loadUrl("file:///android_asset/html_test/video.html");

第一视频作品,第二个没有。我尝试了不同的值的,他们都没有为我工作:

The first video works, the second one doesn't. I tried different values for the source, neither of them worked for me:

<source src="BigBuck.m4v" />
<source src="file:///android_asset/html_test/BigBuck.m4v" />

不知道这是否是相关的,但只要我preSS发挥,的logcat 拿出这样的:

01-07 12:19:18.073: E/MediaPlayer(32542): error (1, -2147483648)
01-07 12:19:18.073: E/MediaPlayer(32542): Error (1,-2147483648)

我不知道这个问题是在这里什么。任何帮助将是非常美联社preciated。

I have no clue what the problem is here. Any help would be much appreciated.

推荐答案

Slartibartfasts帮我解决这个问题。因为它只是一个评论,我会发布它自己。

Slartibartfasts helped me solve this. Since it's just a comment, I'll have to post it myself.

本质上,它是一个权限问题。 这是他张贴的链接。与 MODE_WORLD_READABLE 复制文件建议的方法是pcated德$ P $,请使用下面的链接中描述的方法,并保存在外部存储器中的文件

Essentially it's a permission problem. Here is the link he posted. The suggested method for copying files with MODE_WORLD_READABLE is deprecated, use the method described in the link below and save the files in the external storage

我不得不将文件复制到外部存储和访问视频文件存在。自的路径外部存储是几个版本机器人中的不同,我复制所有相关的HTML文件(包括所有的JS,图形和视频文件)。请参阅<一href="http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard">copying文件,向下滚动的解答,如果您有子文件夹。

I had to copy the files to the external storage, and access the video file there. Since the path to the external storage is different within the several versions of android, I copied the all relevant HTML files (including all JS, graphics and video files). See copying files, scroll down for the answer if you have sub folders.

下面是我的复制方法:

private void copyAssets(String path)
{
    String[] files = null;

    try
    {
        files = getAssets().list(path);
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    if (files.length == 0)
        copyFile(path);
    else
    {
        File dir = new File(getExternalFilesDir(null), path);

        if (!dir.exists())
            dir.mkdir();

        for (int i = 0; i < files.length; i++)
        {
            copyAssets(path + "/" + files[i]);
        }
    }
}

private void copyFile(String filename)
{
    InputStream in = null;

    File file;
    OutputStream out = null;

    try
    {
        in = getAssets().open(filename);
    } catch (IOException e)
    {
        Log.e(TAG, "ERROR WITH in = getAssets().open: " + filename);
        e.printStackTrace();
    }

    file = new File(getExternalFilesDir(null), filename);

    try
    {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e)
    {
        Log.e(TAG, "ERROR WITH out = new FileOutputStream(file);");
        e.printStackTrace();
    }

    byte[] data;

    try
    {
        data = new byte[in.available()];

        in.read(data);
        out.write(data);

        in.close();
        out.close();
    } catch (IOException e1)
    {
        e1.printStackTrace();
    }
}

在我的的onCreate 我称之为

copyAssets("matrix");

与基体是在我的资产的文件夹,多数民众赞成持有的所有文件和子文件夹。

with matrix being the folder in my assets thats holding all files and sub folders.

这篇关于在Android中的WebView播放本地视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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