Javascript 我可以将媒体对象从ionic转换为Blob吗?
我尝试将Ionic中记录的MediaObject转换为Blob,但没有成功。 有人知道怎么做吗Javascript 我可以将媒体对象从ionic转换为Blob吗?,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我尝试将Ionic中记录的MediaObject转换为Blob,但没有成功。 有人知道怎么做吗 您需要首先获取文件的路径,然后将其转换为blob 获取文件路径: 为了避免一些iOS怪癖,我首先使用文件插件的createFile()方法创建一个临时文件。它返回一个承诺,该承诺解析为您需要通过toInternalURL()方法将其转换为cordova(CDV)文件路径的路径。介质对象是在创建临时文件后创建的。它不应该有“file://”前缀。e、 g this.filePlugin.createF
您需要首先获取文件的路径,然后将其转换为blob
createFile()
方法创建一个临时文件。它返回一个承诺,该承诺解析为您需要通过toInternalURL()
方法将其转换为cordova(CDV)文件路径的路径。介质对象是在创建临时文件后创建的。它不应该有“file://”
前缀。e、 g
this.filePlugin.createFile(this.file.tempDirectory, YOUR_FILE_NAME, true).then(filePath => {
audio: MediaObject = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + YOUR_FILE_NAME);
// your recording logic here...
// once the recording is finished:
cdvPath = filePath.toInternalURL();
}
cdvPath
调用它,就完成了。(请注意它解析为对象的事实!):你能添加一些代码吗?你试过这个了吗。audioBlob=newblob([this.audio],{type:'audio/mp3'});
this.filePlugin.createFile(this.file.tempDirectory, YOUR_FILE_NAME, true).then(filePath => {
audio: MediaObject = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + YOUR_FILE_NAME);
// your recording logic here...
// once the recording is finished:
cdvPath = filePath.toInternalURL();
}
makeFileIntoBlob(_filePath) {
return new Promise((resolve, reject) => {
let fileName, fileExtension = "";
this.file.resolveLocalFilesystemUrl(_filePath)
.then(fileEntry => {
let {name, nativeURL} = fileEntry;
// get the path..
let path = nativeURL.substring(0, nativeURL.lastIndexOf("/"));
fileName = name;
// if you already know the file extension, just assign it to // variable below
fileExtension = fileName.match(/\.[A-z0-9]+$/i)[0].slice(1);
// we are provided the name, so now read the file into a buffer
return this.file.readAsArrayBuffer(path, name);
})
.then(buffer => {
// get the buffer and make a blob to be saved
let medBlob = new Blob([buffer], {
type: `audio/${fileExtension}`
});
// pass back blob and the name of the file for saving
// into fire base
resolve({ blob: medBlob, blobName: name});
})
.catch(e => reject(e));
});
}