Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

html - Playing local video in WebView on Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...