gpt4 book ai didi

javascript - PhoneGap FileReader/readAsDataURL 不触发回调

转载 作者:行者123 更新时间:2023-11-30 05:32:56 26 4
gpt4 key购买 nike

我正在使用 PhoneGap Build 构建 iOS v7.1+ 应用程序并使用 weinre调试。我正在使用媒体捕获插件和文件 API 来捕获视频以尝试获取其 base64 表示形式。我可以让录像机打开,拍摄视频,并返回文件路径。然后,我使用 resolveLocalFileSystemURL() 获取 readAsDataURL() 需要的文件对象。问题是 FileReader 永远不会调用 onloadend 回调。

我整天都在闲逛。将 console.log() 放在我能想到的任何地方。我检查以确保 iOS 版本 is supported .每个变量都是我所期望的,但根本没有调用回调。我也试过设置所有其他回调,但也没有一个被调用过。我尝试用 readAsText() 替换 readAsDataURL() 但我仍然得到 bupkis。我已经尝试等待最多五分钟,因为我认为异步调用可能需要一点时间,但仍然没有。

下面是我的代码。下面是控制台输出。

var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];

navigator.device.capture.captureVideo(
function(files) {
for ( var i in files ) {
var file = files[i];

var name = file.name;
var path = file.fullPath;
if ( path.indexOf("/private") === 0 )
path = "file://" + path.substr(8);
else
path = "file://" + path;
var type = file.type;
var lastModifiedDate = file.lastModifiedDate;
var size = file.size;

var reader = new FileReader();
reader.onloadend = function(event) {
console.log(3);
elements["video"].type = type;
elements["video"].src = "data:" + type + ";base64," + event.target.result;
console.log(4);
};

window.resolveLocalFileSystemURL(
path,
function(entry) {
console.log(1, entry.nativeURL);
reader.readAsDataURL(entry);
console.log(2);
},
function(error) {
console.log("0-0", error);
}
);
}
},
function(error) {
console.log("0-1", error);
},
{
limit: 1
}
);

1 "file:///var/mobile/Applications/AB239984-FB9F-43C0-B699-3596AC8A43A8/tmp/capture/capturedvideo.MOV"

2

最佳答案

重新组织一下您的代码。将 reader 初始化和 onloadend 回调放在 resolveLocalFileSystemURL 成功回调中。

像这样:

var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];

navigator.device.capture.captureVideo(
function(files) {
for ( var i in files ) {
var file = files[i];

var name = file.name;
var path = file.fullPath;
if ( path.indexOf("/private") === 0 )
path = "file://" + path.substr(8);
else
path = "file://" + path;
var type = file.type;
var lastModifiedDate = file.lastModifiedDate;
var size = file.size;

window.resolveLocalFileSystemURL(
path,
function(entry) {
console.log(1, entry.nativeURL);
var reader = new FileReader();
reader.onloadend = function(event) {
console.log(3);
elements["video"].type = type;
elements["video"].src = "data:" + type + ";base64," + event.target.result;
console.log(4);
};
reader.readAsDataURL(entry);
console.log(2);
},
function(error) {
console.log("0-0", error);
}
);
}
},
function(error) {
console.log("0-1", error);
},
{
limit: 1
}
);

关于javascript - PhoneGap FileReader/readAsDataURL 不触发回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com