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

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

问题描述

我正在构建一个带有 WebView 的应用程序,该应用程序应该播放视频,并保存在本地.奇怪的是,视频播放器不能处理本地视频文件.不过,它确实可以播放保存在服务器上的视频.

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

这是文件.

HTML

<p>服务器</p><video poster="video/star.png" 控件><source src="http://broken-links.com/tests/media/BigBuck.m4v"/></视频>

<div class="video-container"><p>本地</p><video poster="video/star.png" 控件><source src="BigBuck.m4v"/></视频>

在活动中创建

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");

第一个视频有效,第二个无效.我为 source 尝试了不同的值,但它们都不适合我:

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

不确定这是否相关,但我一按下播放键,logcat 就会显示:

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

我不知道这里有什么问题.任何帮助将不胜感激.

解决方案

Slartibartfasts 帮我解决了这个问题.既然只是评论,那我就得自己贴了.

本质上这是一个权限问题.这是他发布的链接.不推荐使用 MODE_WORLD_READABLE 复制文件的建议方法,请使用下面链接中描述的方法并将文件保存在外部存储中

我必须将文件复制到外部存储器,然后在那里访问视频文件.由于android几个版本的外部存储路径不同,我把所有相关的HTML文件(包括所有的JS、图形和视频文件)都复制了下来.请参阅复制文件,如果你有子文件夹.

这里是我的复制方法:

private void copyAssets(String path){String[] 文件 = null;尝试{文件 = getAssets().list(path);} catch (IOException e){e.printStackTrace();}if (files.length == 0)复制文件(路径);别的{File dir = new File(getExternalFilesDir(null), path);如果 (!dir.exists())目录.mkdir();for (int i = 0; i 

在我的 onCreate 中调用

copyAssets("矩阵");

矩阵是我资产中的文件夹,其中包含所有文件和子文件夹.

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.

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

Here are the files.

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 in Activity

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" />

Not sure if this is related, but as soon as I press play, logcat puts out this:

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

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

解决方案

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

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

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.

Here are my copying methods:

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

In my onCreate I call

copyAssets("matrix");

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

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

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