angular - 如何使用 ionic angular 项目的插件将音频文件转换为 base64

标签 angular ionic-framework plugins base64

我正在尝试从媒体捕获 ionic 插件中获取录音并将文件转换为 base64 字符串。我附上了适用于 Android 但不适用于 IOS 或 Microsoft 的代码。

public captureAudio(name: string, notes: string): void {

    this.mediaCapture.captureAudio().then(res => {
        var filePath = res[0].fullPath;

        this.getBase64StringByFilePath(filePath)
            .then((res) => {
                var base64Only = res.slice(34);
                //do something with base64 string
            });
    });
}

public getBase64StringByFilePath(fileURL: string): Promise<string> {

    return new Promise((resolve, reject) => {
        this.base64.encodeFile(fileURL).then((base64File: string) => {
            resolve(base64File);
        }, (err) => {
            console.log(err);
        });
    })
}

我注意到在我使用的 base64 转换插件中声明:

“获取任何图像的 base64 编码的插件,可以为 android 的任何文件检索 Base64,但仅支持 iOS 图像”

还有其他选择吗?我们也查看了下面的文件插件,但无法向其发送所需的确切文件路径(它要么不返回任何内容,要么返回错误)。我们还尝试了使用 getUserMedia 的更原生的 javascript 解决方案,但由于安全原因,它也不适用于 IOS。

使用的插件:

最佳答案

此函数已从在线教程中获取 [ https://medium.com/@JordanBenge/ionic-converting-video-to-base64-a95158de3b2a]

读取 fileURI,使用 FileReader() 将其编码为 base64,然后我们必须修复它。 @param aAudioRecording 采集视频的视频文件源(URI)

    private async convertAudioToBase64(aAudioRecording): Promise<{}> {
        return new Promise(async resolve => {
            let lAudioSource: any = await this.file.resolveLocalFilesystemUrl(aAudioRecording);

            lAudioSource.file(resFile => {
                let lReader = new FileReader();
                lReader.readAsDataURL(resFile);
                lReader.onloadend = async (evt: any) => {
                    let lEncodingType: string;

                    if (this.dataProvider.getPlatform() == "ios") {
                        lEncodingType = "data:audio/mp3;base64,";
                    } else {
                        lEncodingType = "data:audio/x-m4a;base64,";
                    }

                    /*
                     * File reader provides us with an incorrectly encoded base64 string.
                     * So we have to fix it, in order to upload it correctly.
                     */
                    let lOriginalBase64 = evt.target.result.split(",")[1]; // Remove the "data:video..." string.
                    let lDecodedBase64 = atob(lOriginalBase64); // Decode the incorrectly encoded base64 string.
                    let lEncodedBase64 = btoa(lDecodedBase64); // re-encode the base64 string (correctly).
                    let lNewBase64 = lEncodingType + lEncodedBase64; // Add the encodingType to the string.

                    resolve(lNewBase64);
                };
            });
        });
    }

关于angular - 如何使用 ionic angular 项目的插件将音频文件转换为 base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063393/

相关文章:

html - ionic 4 ion-select-option 文本换行

pdf - 如何仅使用 Adob​​e PDF 插件强制在谷歌浏览器中显示 PDF

python - Sublime Text插件设置: Not Loading

angularjs - ngCordova Bower 错误 - 有什么解决办法吗?

ios - 构建 iOS 失败 - Cordova Ionic iOS

grails - Grails 2.4.4 上的导出插件

angular - 类型 'zip' 上不存在属性 'typeof Observable'。 Angular 6

angular - 所需的垫选择无法重置

angular - 类型 'forRoot' .ts(2339) 上不存在属性 'typeof NgbModule'

angular - 找不到名称 'PropertyKey'