无法使用 Ionic 的媒体插件在音频文件中创建语音消息
Unable to create voice message in audio file using Ionic's Media plugin
我的应用程序中有聊天组件,我正在尝试添加发送语音消息(如 WhatsApp)功能。我正在使用 Ionic 的媒体插件和 MediaObject 类型。该应用程序适用于 Android 和 iOS。目前,我正在 Android 上进行测试,但没有成功!
我尝试了下面的代码来创建一个 MediaObject 并将录制的音频存储在其中。如果用户再次单击该按钮,我想停止录制。它正在工作,因为我在控制台上获得了日志,但是 getDuration()
returns -1!
startRecording() {
if (!this.recording) {
console.log('Started Recording');
this.recording = true;
this.fileName = Date.now();
this.audio = this.media.create(`../../../../assets/chat/${this.fileName}.m4a`);
this.audio.startRecord();
this.listenToAudioEvents();
} else {
this.stopRecording();
}
}
stopRecording() {
this.audio.stopRecord();
console.log(this.audio);
this.recording = false;
this.audioReady = true;
this.audio.getDuration();
console.log('Audio Duration: ' + this.audio.getDuration());
}
我想在 stopRecording()
被调用时停止录音并保留音频文件以便我可以上传它。另外,我不太确定文件实际存储在 Android 或 iOS.
上的位置
基本上,问题出在我保存生成的媒体文件的位置。首先,我需要使用 Ionic Native 的 File 插件。然后在 File.tempDirectory
处为 iOS 创建媒体文件,在 File.externalDataDirectory
处为 Android 创建媒体文件:
if (this.platform.is('ios')) {
await this.file.createFile(this.file.tempDirectory, this.fileName, true);
this.filePath = this.file.tempDirectory + this.fileName;
this.audio = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + this.fileName);
} else if (this.platform.is('android')) {
this.filePath = this.file.externalDataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
}
this.audio.startRecord();
注意 iOS 的额外步骤也很重要,如页面底部 here 所述。然后我就可以像这样访问 blob:
this.file.readAsDataURL(this.filePath.replace(this.fileName, ''), this.fileName).then(blob => {
console.log(blob);
}
我的应用程序中有聊天组件,我正在尝试添加发送语音消息(如 WhatsApp)功能。我正在使用 Ionic 的媒体插件和 MediaObject 类型。该应用程序适用于 Android 和 iOS。目前,我正在 Android 上进行测试,但没有成功!
我尝试了下面的代码来创建一个 MediaObject 并将录制的音频存储在其中。如果用户再次单击该按钮,我想停止录制。它正在工作,因为我在控制台上获得了日志,但是 getDuration()
returns -1!
startRecording() {
if (!this.recording) {
console.log('Started Recording');
this.recording = true;
this.fileName = Date.now();
this.audio = this.media.create(`../../../../assets/chat/${this.fileName}.m4a`);
this.audio.startRecord();
this.listenToAudioEvents();
} else {
this.stopRecording();
}
}
stopRecording() {
this.audio.stopRecord();
console.log(this.audio);
this.recording = false;
this.audioReady = true;
this.audio.getDuration();
console.log('Audio Duration: ' + this.audio.getDuration());
}
我想在 stopRecording()
被调用时停止录音并保留音频文件以便我可以上传它。另外,我不太确定文件实际存储在 Android 或 iOS.
基本上,问题出在我保存生成的媒体文件的位置。首先,我需要使用 Ionic Native 的 File 插件。然后在 File.tempDirectory
处为 iOS 创建媒体文件,在 File.externalDataDirectory
处为 Android 创建媒体文件:
if (this.platform.is('ios')) {
await this.file.createFile(this.file.tempDirectory, this.fileName, true);
this.filePath = this.file.tempDirectory + this.fileName;
this.audio = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + this.fileName);
} else if (this.platform.is('android')) {
this.filePath = this.file.externalDataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
}
this.audio.startRecord();
注意 iOS 的额外步骤也很重要,如页面底部 here 所述。然后我就可以像这样访问 blob:
this.file.readAsDataURL(this.filePath.replace(this.fileName, ''), this.fileName).then(blob => {
console.log(blob);
}