diff --git a/src/core/features/emulator/components/capture-media/capture-media.ts b/src/core/features/emulator/components/capture-media/capture-media.ts index 4d7fabd8e..2345ade66 100644 --- a/src/core/features/emulator/components/capture-media/capture-media.ts +++ b/src/core/features/emulator/components/capture-media/capture-media.ts @@ -130,7 +130,27 @@ export class CoreEmulatorCaptureMediaComponent implements OnInit, OnDestroy { * @return Promise resolved when ready. */ protected async initCordovaMediaPlugin(): Promise { + + try { + await this.createFileAndMediaInstance(); + + this.readyToCapture = true; + this.previewMedia = this.previewAudio?.nativeElement; + } catch (error) { + this.dismissWithError(-1, error.message || error); + } + } + + /** + * Create a file entry and the cordova media instance. + */ + protected async createFileAndMediaInstance(): Promise { this.filePath = this.getFilePath(); + + // First create the file. + this.fileEntry = await CoreFile.createFile(this.filePath); + + // Now create the media instance. let absolutePath = CoreText.concatenatePaths(CoreFile.getBasePathInstant(), this.filePath); if (Platform.is('ios')) { @@ -138,17 +158,21 @@ export class CoreEmulatorCaptureMediaComponent implements OnInit, OnDestroy { absolutePath = absolutePath.replace(/^file:\/\//, ''); } - try { - // First create the file. - this.fileEntry = await CoreFile.createFile(this.filePath); + this.mediaFile = Media.create(absolutePath); + } - // Now create the media instance. - this.mediaFile = Media.create(absolutePath); - this.readyToCapture = true; - this.previewMedia = this.previewAudio?.nativeElement; - } catch (error) { - this.dismissWithError(-1, error.message || error); + /** + * Reset the file and the cordova media instance. + */ + protected async resetCordovaMediaCapture(): Promise { + if (this.filePath) { + // Remove old file, don't block the user for this. + CoreFile.removeFile(this.filePath); } + + this.mediaFile?.release(); + + await this.createFileAndMediaInstance(); } /** @@ -410,11 +434,15 @@ export class CoreEmulatorCaptureMediaComponent implements OnInit, OnDestroy { /** * Discard the captured media. */ - discard(): void { + async discard(): Promise { this.previewMedia?.pause(); this.streamVideo?.nativeElement.play(); this.audioDrawer?.start(); + if (this.isCordovaAudioCapture) { + await this.resetCordovaMediaCapture(); + } + this.hasCaptured = false; this.isCapturing = false; this.resetChrono = true;