android - 离子 V5 电容器 - cordova-plugin-media 错误代码 1
问题描述
我已经安装了插件并检查了所有需要的权限,但仍然在媒体插件上收到错误代码 1。
我也加<application android:requestLegacyExternalStorage="true" />
了<edit-config>
。
版本
- 离子原生/应用程序版本:“^5.33.1”
- 电容器/cli:“^2.4.8”
- 电容器/机器人:“^2.4.6”
- 科尔多瓦插件媒体:“^5.0.3”
- @ionic-native/文件:“^5.34.0”
代码示例:
startRecording() {
if (this.platform.is('ios')) {
this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.mp3';
this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + this.fileName;
this.audio = this.media.create(this.filePath);
} else if (this.platform.is('android')) {
this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.mp3';
this.filePath = this.file.externalDataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
}
this.audio.onStatusUpdate.subscribe(status => {
console.log("status", status)
});
this.audio.onSuccess.subscribe(() => {
console.log("success")
});
this.audio.onError.subscribe(error => {
console.log("error", error)
});
this.audio.startRecord();
this.recording = true;
}
解决方案
Since Android 11+ (SDK 30+) you can't write files to Documents or ExternalStorage.
Change the DIR you write your files to. (this.filePath)
See: https://developer.android.com/about/versions/11/privacy/storage