Crashing app for no reason - Ionic 4

Asked

Viewed 79 times

0

I am making an application (Ionic4, typescript, Node.js) for recording and playing audio where the user records the sound and after the audio is presented with a play button; Soon after the user gives permission to store data and access the microphone, the application closes without any error in the Debugger.

Follow the code of . html and . ts

Audios.page.html

  <ion-row>
    <ion-col>
      <ion-button expand="full" (click)="capturarAudio()">Gravar áudio</ion-button>
    </ion-col>
  </ion-row>

  <ion-list>

    <ion-item *ngFor="let arquivo of mediaFiles" tappable (click)="play(arquivo)" text-wrap>
    {{ file.name }}
    <p>{{file.size / 1000 / 1000 | number }} MB </p>
    </ion-item>
  </ion-list> 

Audios.pagets.

    import { Component, OnInit } from '@angular/core';
    import { MediaCapture } from '@ionic-native/media-capture/ngx';
    import { IonicStorageModule } from '@ionic/storage';
    import { Media, MediaObject  } from '@ionic-native/media/ngx';
    import { File } from '@ionic-native/file/ngx';
    import { Storage } from '@ionic/storage';

    const MEDIA_FILES_KEY = 'mediafiles';

    @Component({
      selector: 'app-audios',
      templateUrl: './audios.page.html',
      styleUrls: ['./audios.page.scss'],
    })
    export class AudiosPage implements OnInit {

      mediaFiles = [];
      constructor(private mediaCapture: MediaCapture, private storage: Storage, private media: Media, private file: File) { 


      }

      ionViewDidLoad(){

        this.storage.get(MEDIA_FILES_KEY).then(res => {
          this.mediaFiles = JSON.parse(res) || [];
        });
      }

      capturarAudio(){
        this.mediaCapture.captureAudio().then( res => {
          this.storeMediaFiles(res);
        })
      }

      play(myFile){
        console.log('play', myFile);
        if (myFile.name.indexOf('.wav') > -1){
          const audioFile: MediaObject = this.media.create(myFile.localURL)
          audioFile.play();
        }
      }

      storeMediaFiles(files){
        console.log('storage:', files);
        this.storage.get(MEDIA_FILES_KEY).then(res => {
          if (res) {
            let arr = JSON.parse(res);
            arr = arr.concat(files);
            this.storage.set(MEDIA_FILES_KEY, JSON.stringify(arr))
          } else {
            this.storage.set(MEDIA_FILES_KEY, JSON.stringify(files))
          }
          this.mediaFiles = this.mediaFiles.concat(files);
        })

      }

        ngOnInit() {
  }

}

EDIT1: Taking a look at Logcat I get this error:

E/PluginManager: Uncaught exception from plugin
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.MediaStore.RECORD_SOUND }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1899)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1589)
        at android.app.Activity.startActivityForResult(Activity.java:4229)
        at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:343)
        at android.app.Activity.startActivityForResult(Activity.java:4187)
        at org.apache.cordova.CordovaInterfaceImpl.startActivityForResult(CordovaInterfaceImpl.java:68)
        at org.apache.cordova.mediacapture.Capture.captureAudio(Capture.java:234)
        at org.apache.cordova.mediacapture.Capture.execute(Capture.java:132)
        at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
        at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
        at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
        at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)
  • This may have to do with the plugin you installed. Installed some?

  • Only the necessary, media-capture.

  • You are testing on a mobile phone or in the Browser ?

  • I’m running on mobile, I tested in two and both happens the same thing

1 answer

0

The generated Exception explains the error:

  • E/PluginManager: Uncaught exception from plugin: means that the plugin manager detected a flaw not dealt with by the plugin (which may indicate a problem in it);
  • android.content.ActivityNotFoundException: this exception occurs when an Activity is not found to resolve a Intent. In practice this occurs when you try to launch an application or service that does not exist on the system - it may simply not be installed;
  • No Activity found to handle Intent { act=android.provider.MediaStore.RECORD_SOUND }: this is the detail of ActivityNotFoundException, indicating that no Activity has been found to satisfy Intent.

The error happens because the system was not able to open the sound recorder app. I believe it will be necessary to implement the functionality through another plugin - this should fit.

If you don’t want to implement this feature, you can just check if there is a recording app installed. On Android, in java code, this can be done this way:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
    Log.d(TAG, "Não foi possível iniciar a Intent");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.