Capturing audio in an Ionic 4 app with the help of the media-capture Cordova plugin

I am facing an issue with recording audio in my Ionic 4 app using the media-capture Cordova plugin. Whenever I press the record button, it launches an external recorder app to capture the audio, which I can then play within my app. However, I want to be able to record the audio directly within my app without relying on an external tool. Can anyone guide me on how to achieve this?

Below is the code snippet from my home.page.html file:

<ion-header>
  <ion-toolbar>
    <ion-title>
      Audio Testing App
    </ion-title>
  </ion-toolbar>
</ion-header>

... (omitted for brevity)

In my home.page.ts file:

(Javascript and TypeScript code here)

Screenshots of the app: Image 1: Home Page

https://i.sstatic.net/PBjDh.jpg

Image 2: External Recorder - Opens when clicking the record button

(Would like to avoid this step and record audio directly inside the app)

https://i.sstatic.net/xoMMF.jpg

Answer №1

No need to be concerned about this issue. You have the option to easily record audio using the application.

startRecordingAudio() {
  try {
    let fileName =
    'recording' +
    new Date().getDate() +
    new Date().getMonth() +
    new Date().getFullYear() +
    new Date().getHours() +
    new Date().getMinutes() +
    new Date().getSeconds();
    
    if (this.platform.is('ios')) {
      fileName = fileName + '.m4a';
      this.audioPath = this.file.documentsDirectory + fileName;
      this.audio = this.media.create(this.audioPath.replace(/file:\/\//g, ''));
    } else if (this.platform.is('android')) {
      fileName = fileName + '.mp3';
      this.audioPath = this.file.externalDataDirectory + fileName;
      this.audio = this.media.create(this.audioPath.replace(/file:\/\//g, ''));
    }
    this.audio.startRecord();
    this.isAudioRecording = true;
  } catch (error) {
    console.log(error);
  }
}

stopRecordingAudio() {
  this.audio.stopRecord();
  this.isAudioRecording = false;
}

playRecordedAudio() {
  try {
    this.audio = this.media.create(this.audioPath);
    this.audio.play();
    this.audio.setVolume(0.8);
  } catch (error) {
    console.log(error);
  }
}

Remember to include

<application android:requestLegacyExternalStorage="true" />

in your AndroidManifest.xml file

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to implement an edit feature in an HTML Vue.js application that is stored within an array

This is the JSON response I received: {"status":true,"data":[{"_id":"5afd20c8aae8bd215cc3c33e","no":131,"Date":"2000-01-01T00:00:00.000Z","__v":0,"rules":[{"name":"Act,1972","section":"12","_id":"5afd20c8aae8bd215cc3c341","data":[{"head":"no","value":""," ...

Storing an existing item from shared preferences into an array list in an Android application

I have a pre-existing array list stored in shared preferences, and I want to add an additional item to it. However, whenever I try to do so, it ends up removing all the data that was previously stored in the array list. Is there a way for me to add an ite ...

JavaScript form validation involves using client-side scripting to ensure that

I'm currently working on form validation, but I'm unsure if I'm overcomplicating things. I have a variable that compares elements and replaces an image with a checkmark for success or an X for failure upon successful validation. The function ...

Error in overwritePermissions caused by a bug

message.guild.channels.create(`ticket-${message.author.username}`).then (channel => channel.overwritePermissions(Support, { SEND_MESSAGES: true, READ_MESSAGES: true ...

What is the reason behind classList returning 'undefined' unless getElementById is explicitly used?

Why is it that I can't seem to select multiple elements with the same class (using querySelector, querySelectorAll, getElementsByClassName, etc) and use classList to check if just one div contains a specific class? I've come across tutorials tha ...

The functionality of the page becomes disrupted when multiple timers are used after the setInterval function is executed in

My webpage fetches multiple data for a table, with one of the columns displaying date and time values from which countdown timers run. Each row has its own timer and functions correctly. However, when I added JavaScript code to refresh the table every 10 ...

Is it possible to hide Form elements and remove their values simultaneously?

I have implemented a script to display an additional language menu, which functions perfectly. However, I encountered an issue where if the user selects a language and then decides to remove it by clicking on "Remove", the field is hidden but the value sti ...

Having trouble initiating a "curl:localhost:3000" connection, receiving a URI Error message

Recently delving into the realm of node js, I have embarked on a journey to start up a server and experiment with an app designed to trim URLs. However, I find myself at an impasse. Environment: Windows Text Editor: VSCode Below is my code for index.js ...

What is the best way to navigate to a specific location on a web page?

After clicking the "Add comment" link, a comment form popped up using Ajax. I now need assistance with scrolling to it, can you please help me out? ...

How to detect changes in a variable within a different component using Angular 7

My navbar component includes a dropdown menu that allows users to change the language using ngx/translate: <div class="traduction"> <ul> <li class="nav-item dropdown no-arrow"> <a class="nav-link dropdown ...

To include the express module in another JavaScript file, simply require it at the top of the file using the following code: `

Let me elaborate on my current project setup. I have integrated the express module in my app.js. Now, I need to access this module in other files such as index.js, admin.js, and gallery.js. However, I do not want to repeat the same code in each of these fi ...

Replacing the tbody element in React with centered text using inline styles ---If you want

I am working with an empty array in React that I translate into state. When the array is empty, I want to insert a text that says "no match events yet..." into a react-bootstrap Table's tbody. In the current setup, I am struggling to center the text ...

The final marker is consistently used by the click event

Currently exploring the Google Maps API for the first time and facing a challenge in Rails when trying to add a click event for each marker. I am looping through the team_locations model to extract latitude and longitude data in order to set up markers. ...

Assign an attendee the role of organizer in the Google Calendar API

I am currently utilizing the googleapis npm package to facilitate calendar event creation. Below is my request payload for calendar.events.insert in order to generate an event: { "summary":"Biology class", "location":"Google Meet: Please follow the go ...

Error (2322) Occurs When TypeScript Class Implements Interface with Union Type Field

I'm having trouble with error code 2322 popping up unexpectedly. Could you please take a look at this code snippet for me? interface Fish { alive: string | boolean; } class FishClass implements Fish { alive = 'Yes' constructor() { ...

Deciphering WHERE conditions in a Node.js environment

Looking to convert the WHERE clause of a MySQL query into an expression for extracting data from a JSON file. For instance: `price`=8.95 AND `title`="The Lord price= AND of the Rings" The above example should transform into the following string after su ...

Adjust the CSS of a dynamically generated jQuery checkbox button in real-time

I am working on a project where I am creating a series of jQuery checkboxes dynamically within a loop. Here is how I am doing it: var checkbox = $('<input>').attr({type: 'checkbox', id: checkbox_id); panel.append(checkbox); panel ...

Is there a way to attach an event for multiple arithmetic operations to a checkbox?

My form includes 4 checkboxes for different mathematical operations. <form action="" method="POST"> Select number of questions: <input type="number" name="que" value="que"> <br> <br> Select number of series: <select name="sel ...

Validating classes in Node.js using class-validator

I'm having some trouble with the class-validator package in my Node project. It's not being recognized and throwing an error. @MinLength(10, { ^ SyntaxError: Invalid or unexpected token Here's an example of what I'm doing: co ...

Bundling extraneous server code in client rollup

Can someone help me understand why the following code is being included at the top of my browser bundle by Rollup? It seems like these references are not used anywhere from my entry point. Could it be default includes from node builtins? import require$$0$ ...