jaeger25 / Html5Video

A Phonegap plugin that allows playback of local video files using html 5 video tags
Apache License 2.0
99 stars 65 forks source link

Use the plugin in conjunction with apkfile expansion #36

Open piermariacosina opened 9 years ago

piermariacosina commented 9 years ago

Considering that the videos are the heaviest part of the app we are considering moving them in the apk file expansion using this plugin https://github.com/agamemnus/cordova-plugin-xAPKreader

But there is a question we can't solve the file are now on res/raw they should be moved inside the file expansion, is it possible to manage it using the video plugin ?

gylippus commented 9 years ago

@piermariacosina I'd doubt it was the 100% recommended approach, but we have been able to do this with a bit of updating to the .java file. I'd recommend retrieving your contents name dynamically instead of hard coding it in like I did. I haven't gotten around to fixing that quite yet.

package org.apache.cordova.plugin;

import java.io.File;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.Uri;

public class Html5Video extends CordovaPlugin {
    public final String ACTION_INITIALIZE = "initialize";
    public final String ACTION_PLAY = "play";

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {

        boolean status = false;
        Context context = cordova.getActivity();

        if (action.equals(ACTION_INITIALIZE)) {
            JSONObject videos = args.getJSONObject(0);
            JSONArray tagNames = videos.names();
            JSONObject convertedVideos = new JSONObject();

            if (tagNames != null) {
                for (int i = 0; i < tagNames.length(); i++) {
                    String[] video = videos.getString(tagNames.getString(i)).split("\\.");
                    String externalPath = toDirUrl(context.getExternalFilesDir(null));
                    //THIS WILL DEFINITELY BE DIFFERENT FOR YOU
                    convertedVideos.put(tagNames.getString(i), "content://com.test.expansion/assets/videos/" + video[0] + ".mp4");
                    LOG.d("Html5Video", "Id: " + tagNames.getString(i) + " , src: " + convertedVideos.getString(tagNames.getString(i)));
                }

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, convertedVideos));                
                status = true;
                convertedVideos = new JSONObject();
            }
        } else if (action.equals(ACTION_PLAY)) {
            final String videoId = args.getString(0);
            if (videoId != null) {
                cordova.getActivity().runOnUiThread(new Runnable() {
                     public void run() {
                           webView.loadUrl("javascript:window.plugins.html5Video._play(" + videoId + ")");
                     }
                });
                status = true;

                LOG.d("Html5Video", "Playing video with id: " + videoId);
            }
        }

        return status;
    }

    @Override
    public Object onMessage(String id, Object data) {
        return super.onMessage(id, data);
    }

    public String toDirUrl(File f) {
        return Uri.fromFile(f).toString() + '/';
    }
}