Ionic backgorund mode crash

1.3k views Asked by At

I am trying to make run a background mode with Ionic, but when I am playing a song, and I enabled the Backgound Mode, the app crashes.

Actually, is posible make the background mode with Ionic?

This is my code:

import { Component } from '@angular/core';
import {NavController} from '@ionic/angular';
import {NativeAudio} from '@ionic-native/native-audio/ngx';
import {BackgroundMode} from '@ionic-native/background-mode/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  constructor(public navCtrl: NavController,
              public nativeAudio: NativeAudio ,
              public backgroundMode: BackgroundMode) {
    this.nativeAudio.preloadSimple('audio1', 'assets/audio1.mp3').then((msg) => {
      console.log('message: ' + msg);
    }, (error) => {
      console.log('error: ' + error);
    });
  }

  public playAudio(){
    this.backgroundMode.enable();
    this.backgroundMode.on('activate').subscribe(() => {
      this.nativeAudio.play('audio1');
    });
    this.nativeAudio.play('audio1', () => console.log('audio1 is done playing'));
  }

}

This is my current config.

/bg-mode-example$ ionic info

Ionic:

   Ionic CLI                     : 6.11.11 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.3.5
   @angular-devkit/build-angular : 0.1000.8
   @angular-devkit/schematics    : 10.0.8
   @angular/cli                  : 10.0.8
   @ionic/angular-toolkit        : 2.3.3

Cordova:

   Cordova CLI       : 10.0.0
   Cordova Platforms : android 9.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.2.1, (and 6 other plugins)

Utility:

   cordova-res (update available: 0.15.1) : 0.6.0
   native-run (update available: 1.2.1)   : 0.2.8

System:

   Android SDK Tools : 26.1.1 (/home/gabriel/Android/Sdk)
   NodeJS            : v10.19.0 (/usr/bin/node)
   npm               : 6.14.8
   OS                : Linux 5.4

Addionationally, I have a gitlab repo https://gitlab.com/gabrielrincon/bgmode-testing/

What is wrong?

Thanks very much.

1

There are 1 answers

1
gabrielrincon On

I can make it.

The problem is a permission. The FOREGROUND_SERVICE.

I add to platforms/android/app/src/main/AndroidManifest.xml and work

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

I hope that it help you