Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: this.notifyListeners is not a function #129

Open
folsze opened this issue Aug 6, 2024 · 4 comments
Open

TypeError: this.notifyListeners is not a function #129

folsze opened this issue Aug 6, 2024 · 4 comments

Comments

@folsze
Copy link

folsze commented Aug 6, 2024

I am getting errors like below

TypeError: this.notifyListeners is not a function

triggered from this method:

Audio.onEnded

after upgrading to

  1. capacitor v6
  2. (ionic v8)
  3. (angular v17)
image

plugin version:

"@capgo/native-audio": "^6.4.4",

Here is my native-audio usage:

import { Injectable } from '@angular/core';
import { getIsSoundEffectsOn$ } from './preferences';
import { NativeAudio } from '@capgo/native-audio';
import { Haptics, ImpactStyle } from '@capacitor/haptics';

@Injectable({
  providedIn: 'root'
})
export class AudioService {

  public isSoundEffectsOn = false;

  constructor() {
    this.preloadAudio('click', 'assets/sounds/click.mp3');
    this.preloadAudio('toggle', 'assets/sounds/toggle.mp3');
    this.preloadAudio('success', 'assets/sounds/success.wav');
    this.preloadAudio('failure', 'assets/sounds/failure.wav');

    getIsSoundEffectsOn$().then(isOn => {
      this.isSoundEffectsOn = isOn;
    }).catch(error => {
      console.error('Error fetching sound effects preference:', error);
    });
  }

  private preloadAudio(assetId: string, assetPath: string): void {
    NativeAudio.preload({
      assetId,
      assetPath,
      audioChannelNum: 1,
      isUrl: false
    }).then(() => {
      console.log(`Preloaded ${assetId} successfully.`);
    }).catch(error => {
      console.error(`Error preloading ${assetId}:`, error);
    });
  }

  private async playAudio(assetId: string) {
    if (this.isSoundEffectsOn) {
      try {
        await NativeAudio.play({ assetId });
        console.log(`Playing ${assetId}.`);
      } catch (error) {
        if (assetId !== 'click') {
          console.error(`Error playing ${assetId}:`, error);
        }
      }
    } else {
      console.log(`Sound effects are off. Not playing ${assetId}.`);
    }
  }

  public click(): void {
    Haptics.impact({ style: ImpactStyle.Light });
    this.playAudio('click');
  }

  public toggle(): void {
    Haptics.impact({ style: ImpactStyle.Light });
    this.playAudio('toggle');
  }

  public success(): void {
    Haptics.impact({ style: ImpactStyle.Medium });
    this.playAudio('success');
  }

  public failure(): void {
    this.playAudio('failure');
  }
}

used with a directive:

import {Directive, HostListener} from '@angular/core';
import {AudioService} from '../services/audio-service';

@Directive({
  selector: '[clickSound]',
  standalone: true,
})
export class ClickSoundDirective {
  constructor(private audioService: AudioService) {
  }
  @HostListener('click', ['$event']) onClick(){
    this.audioService.click();
  }
}
@riderx
Copy link
Contributor

riderx commented Aug 27, 2024

Can you try again i updated

@riderx riderx closed this as completed Aug 27, 2024
@riderx riderx reopened this Aug 27, 2024
@folsze
Copy link
Author

folsze commented Aug 27, 2024

(It's NOT gone)

EDIT:

Seems like the error is still being logged for some reason. It is polluting my console on desktop web that's the whole problem. Not causing any other bad things that I know of

@folsze
Copy link
Author

folsze commented Aug 27, 2024

I edited my comment

@riderx
Copy link
Contributor

riderx commented Aug 27, 2024

Ok i found the issue the listener was loosing the context of "this" try 6.4.11 please :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants