简体   繁体   English

将视频从相机意图传递到 webView

[英]Pass video from camera intent to webView

i am trying to implement a webview that start a video intent, and return the video to a webView.我正在尝试实现启动视频意图的 webview,并将视频返回到 webView。

What i try to do:我尝试做的事情:

1) Java - add webAppInterface that open video capture intent: 1)Java - 添加打开视频捕获意图的webAppInterface:

mWebView = (WebView) findViewById(R.id.webView);
mWebView.addJavascriptInterface(webAppInterface, "Android");

public class WebAppInterface {
    ...
    public void dispatchTakeVideoIntent() {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        takeVideoIntent.putExtra(android.provider.MediaStore.EXTRA_DURATION_LIMIT,10);

        if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
            ((AppCompatActivity) mContext).startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }
    ...

2) JavaScript - Call it from the webview: 2) JavaScript - 从 webview 调用它:

Android.dispatchTakeVideoIntent()

3) Java - Get the Uri, and send path to my webview 3) Java - 获取 Uri,并将路径发送到我的 webview

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == Activity.RESULT_OK) {
        Uri videoUri = intent.getData();
        wView.loadUrl("javascript:test('" + videoUri.getPath() + "')");
    }
}

4) JavaScript - Get the path in my webView 4) JavaScript - 在我的 webView 中获取路径

window.test = (videoUriPath) => {
    ...
}

My question is, how to access the video?我的问题是,如何访问视频?

And maybe there is a totally different way to go about it?也许与 go 有完全不同的方式呢?

Accessing the video means I'll suppose playing the video in webView.访问视频意味着我假设在 webView 中播放视频。 Have a video element in your HTML (suppose id is 'my-video'), then your javascript will be:在您的 HTML 中有一个视频元素(假设 id 是“my-video”),那么您的 javascript 将是:

window.test = (videoUriPath) => {
 var video = document.getElementById('video');
 var source = document.createElement('source');

 source.setAttribute('src', videoUriPath);

 video.appendChild(source);
 video.load();
 video.play();
}

ok i found a solution, its a little overkill, but its working...好的,我找到了一个解决方案,它有点矫枉过正,但它的工作......

1) JAVA: convert the video to bytes array 1)JAVA:将视频转换为字节数组

byte[] bytes;
byte[] data = new byte[16384];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = is.read(data)) != -1) {
   output.write(data, 0, bytesRead);
}
bytes = output.toByteArray();

2) JAVA: send encoded chunks (base64) to the webvView 2)JAVA:将编码的块(base64)发送到webvView

int startIndex = 0;
int chunkSize= 16384;
while(startIndex < bytes.length){
     byte[] newArray = Arrays.copyOfRange(bytes, startIndex, startIndex + chunkSize);
     startIndex = startIndex + chunkSize;
     encodedString = Base64.encodeToString(newArray, Base64.DEFAULT);
     wView.loadUrl("javascript:g_sendFile_f('" + encodedString + "')");
}
wView.loadUrl("javascript:g_sendFile_f('" + "finish" + "')");

3) JAVASCRIPT: receive the encode chunks, combine them, and create blob file 3) JAVASCRIPT:接收编码块,合并它们,创建blob文件

let bytesArrFinal_an = []
window.g_sendFile_f = (msg) =>{
     // last call
     if(msg === "finish"){
         let blob = new Blob(byteArrFinal_an,{type : "video/mp4"})
         this.test_videoUrl = URL.createObjectURL(blob);
         console.log("finish")
         return
      }

      // add bytes to final array
      let bytesArr_an = this.b64toByteArr(msg)
      bytesArrFinal_an = bytesArrFinal_an.concat(bytesArr_an);
      console.log(msg)
}

If someone have a more elegant solution i will be happy the see it!如果有人有更优雅的解决方案,我会很高兴看到它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM