Navigating onRelease event with Ionic2 components - a user's guide

I'm currently working on creating a button functionality similar to the voice note feature in WhatsApp. The idea is that when the user holds down the button, the voice recording starts, and upon releasing the button, any action can be performed.

While exploring the ionic2 documentation, I couldn't find a directive that specifically handles the onRelease event as required.

Fortunately, I came across a code snippet that seems to work correctly for this purpose:

  @Output('long-press') onPress: EventEmitter < any > = new EventEmitter();
  @Output('long-press-up') onPressUp: EventEmitter < any > = new EventEmitter();

  ngOnInit() {
    this.pressGesture = new Gesture(this.htmlElement);
    this.pressGesture.listen();

    this.pressGesture.on('press', (event) => {
      this.recordVoice();
      this.onPress.emit(event);
    });

    this.pressGesture.on('pressup', (event) => {
      this.stopRecording();
      this.onPressUp.emit(event);
    });
  }

COMPONENT

        <button type="button" clear item-right (long-press)="longPressed()" (long-press-up)="longPressReleased()">
          <ion-icon name="mic"></ion-icon>
        </button>

However, there's an issue where holding anywhere on the screen triggers this.recordVoice(), and releasing triggers this.stopRecording(). This behavior needs to be limited to the specific button press only.

To address this concern, adjustments need to be made to the existing code. Any guidance on how to modify it to achieve the desired functionality would be greatly appreciated. Thank you.

Answer №1

In my situation, I also had a similar requirement and found the perfect solution in this plugin: https://github.com/wbhob/ionic-long-press

You can implement it by following these steps:

<button ion-button 
    ion-long-press 
    [interval]="400" 
    (onPressStart)="recordVoice()" 
    (onPressing)="recording()" 
    (onPressEnd)="stopRecording()"></button>

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

Methods in Ionic to call an external JavaScript file from TypeScript

Having a JSON list stored in a JavaScript file, my objective is to filter it using TypeScript before sending the filtered results to the HTML homepage. However, I encountered an issue within the HTML file. It's worth mentioning that when running the ...

Karmic Dilemma: Anticipated a single request meeting the criteria but encountered 2 requests

Hello, I am currently working on writing a unit test case for the subscribe method of a controller in Angular. I seem to be encountering an issue with the second test case, as indicated by the following error message: Error: Expected one matching request ...

Transforming an array of strings into an array: a guide

An API call is returning an object with the following structure: data = { ... filter: "[1,2,3]" ... } I need to convert the string of array into an actual array of numbers, like [1,2,3]. Thank you! ...

What is the best way to modify a particular internal route parameter within Angular 2?

In the midst of creating a versatile calendar that can showcase various types of data, I have devised a unique URL structure to guide me: todo/2017/01/01 showcases daily todos birthdays/2017/01/01 displays birthdays for that particular day todo/2017/01 g ...

The Mat table is not updating on its own

I am facing an issue in my Angular application where a component fetches a hardcoded list from a service and subscribes to an observable to update the list dynamically. The problem arises when I delete an element from the list, as it does not automaticall ...

Developing an asynchronous function to retrieve data from an external API utilizing Await/Async strategy

Currently, there is a method under development that retrieves a value from the API. What steps are needed to properly integrate Async/Await functionality into this process? fetchAccountById(){ let accountName; this.accountService.fetchDa ...

Creating a star-based rating feature through directive implementation

Can anyone help me figure out why my static star rating system using angularjs/ionic is not showing up on the screen? I've been struggling with it and would appreciate some guidance. service.html <ion-list> <ion-item ng-repeat="busine ...

Is it possible to implement cross-field validation with Angular 2 using model-based addErrors?

Currently, I am working on implementing cross-field validation for two fields within a form using a reactive/model based approach. However, I am facing an issue regarding how to correctly add an error to the existing Error List of a form control. Form: ...

Exploring a JSON object using PlaywrightWould you like to know how

Greetings! Here is a snippet of code that I have, which initiates an API call to a specific URL. const [response] = await Promise.all([ page.waitForResponse(res => res.status() ==200 && res.url() == & ...

Tips for modifying the UserRepresentation in keycloak REST API response

Whenever we send a GET request to the following URL: https://my-keycloak/admin/realms/test-realm/users We receive a comprehensive list of users who are associated with the test-realm. According to the Keycloak REST API documentation, this kind of respons ...

What is the best approach for retrieving values from dynamically repeated forms within a FormGroup using Typescript?

Hello and thank you for taking the time to read my question! I am currently working on an Ionic 3 app project. One of the features in this app involves a page that can have up to 200 identical forms, each containing an input field. You can see an example ...

The NgbTooltip fails to display the arrow icon ( ▼ ) within the NgbPopover's window

There appears to be an issue with the arrow div not functioning properly within NgpPopover body due to conflicting arrow classes for NgbPopover's div.arrow and NgbTooltip's div.arrow. This seems to be a known problem in ng-bootstrap when using bo ...

Unable to generate a file on Firebase platform

I've made updates to my firestore rules, and while the simulator is working correctly, I'm encountering an insufficient permissions error when creating a new document. Below are the firebase rules in question: match /users/{usersid} { a ...

Avoid duplication of elements in Angular applications

Currently, I have a component that loads animated divs based on a datasource. *Note: In the example, I've used a lot of <any> because I haven't finalized the model yet. apiService.ts dataArray: Observable<Array<any>>; constru ...

Using Typescript and webpack to detect variables that are defined in the browser but not in Node environment

My goal is to create a package that can be used on both servers and clients with minimal modifications required. Some libraries are available in Node but not in a browser, while others are accessible in a browser but not in Node. For instance, when utili ...

The server encountered an issue with starting the ANCM Out-Of-Process, resulting in HTTP Error 502

We currently have two projects in progress. One involves a Web API built on .NET Core 2.2.6 and an Angular 8 Single Page Application integrated within .NET Core 2.2.6. Both projects have been deployed on IIS 7 with the Web API functioning properly, but the ...

Iterate and combine a list of HTTP observables using Typescript

I have a collection of properties that need to be included in a larger mergeMap operation. this.customFeedsService.postNewSocialMediaFeed(this.newFeed) .mergeMap( newFeed => this.customFeedsService.postFeedProperties( newFeed.Id, this.feedP ...

Guide on displaying tooltip messages for a custom directive in Visual Studio Code

I have developed a custom directive called app-subscriber. When I hover the mouse over it, I want to display a tooltip message saying "This is for subscribers and requires an email address". Is this achievable? Do I need to create a new VS Code extension f ...

Learning the process of configuring neo4j connection details without relying on environment variables

Is there a way to specify the database connection in code using the Drivine neo4j driver without relying on environment variables? ...

Canceling my pending request to BE is triggered by unsubscribing or deleting a component in RxJS

Recently, I've been facing memory leak issues in my angular application. I was advised to include an unsubscribe() call for each subscription to prevent any potential memory leaks. So, I have been incorporating the following code throughout my project ...