Afroza Yasmin
Afroza Yasmin

Reputation: 511

Playing music in background when app closed in Ionic/ngCordova

I have made auto play sound effect, when the current time will be matched with some fixed time. The following code is working well for match time and playing sound when the app is open. But if I turn off my application then the application is not working.

I'm trying to play the sound when my application are closed. However, If my application is closed, Its check the app functionality & It'll be work like as when then app is open.

I'm also study about this issue and get $cordovaNativeAudio, but I'm not sure how can I apply with my functionality.

<-- Ionic code -->

<ion-toggle ng-repeat="item in settingsList track by $index"
                ng-model="item.checked" 
                ng-checked="item.checked">
               {{ item.text }}---{{item.date}}---{{item.time}}
       <div ng-if="item.date === date && item.time === clock">
           <div ng-if="item.checked" ng-init="init1()"></div>
           <div ng-if="!item.checked" ng-init="init2()"></div>
       </div>
    </ion-toggle>

<-- In controller -->

$scope.settingsList = [
                { text: "Wireless", checked: true, date: '2016-06-04', time: '11:12 pm' },
                { text: "Wireless", checked: true, date: '2016-06-04', time: '11:13 pm' },
                { text: "Wireless", checked: true, date: '2016-06-04', time: '11:14 pm' },
                { text: "Wireless", checked: true, date: '2016-06-04', time: '11:15 pm' }
];

var media;

$scope.init1 = function(){
   var sound1 = mediaService.getPathMedia() + 'assets/sound.mp3';
   media = $cordovaMedia.newMedia(sound1);
   media.play();
}
$scope.init2 = function(){
  media.stop();
  media.release();      
}

Upvotes: 0

Views: 2554

Answers (1)

Zahirul Haque
Zahirul Haque

Reputation: 1659

Try this plugin to run the service in background https://github.com/katzer/cordova-plugin-background-mode

and try this plugin to play native audio https://github.com/floatinghotpot/cordova-plugin-nativeaudio

Upvotes: 1

Related Questions