Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic - Playing Audio in background

I am creating one music app which will get songs using API. Songs are playing perfectly but its not working in background. When I close the app audio gets stop. I am creating this app with ionic framework and using cordova Media plugin. I have googled many things but not able to resolve the issue.

Shall I need to use some other plugin instead of cordova Media. Please give some suggestion.

Note: I need only for android devices

like image 981
Sariban D'Cl Avatar asked May 23 '26 13:05

Sariban D'Cl


2 Answers

I didn't get any solution. So I have created one plugin for it. Please refer this link Cordova Plugin - Back As Home

What exactly it's doing?
Instead of closing the app, it's minimizing it. So this code will help back button to work as home button.


Add Plugin

cordova plugin add https://github.com/amitsinha559/cordova-plugin-back-as-home.git

Add this codes in .run if you want to work back as home button

$ionicPlatform.registerBackButtonAction(function(e){
    backAsHome.trigger(function(){
        console.log("Success");
    }, function(){
        console.log("Error");
    });
    e.preventDefault();
},101);

If you want to use in some button action

$scope.someButton = function() {
    backAsHome.trigger(function(){
        console.log("Success");
    }, function(){
        console.log("Error");
    });
}

Please let me know if you are facing any issue.

like image 181
Sariban D'Cl Avatar answered May 25 '26 04:05

Sariban D'Cl


The OS will put the screen and the CPU to sleep unless you acquire a wakelock - Partial wakelock in your case.

There are two attempts to prevent cordova apps from sleeping, or run in the background. Have a look at those plugins.

https://github.com/katzer/cordova-plugin-background-mode

https://github.com/Red-Folder/Cordova-Plugin-BackgroundService

like image 26
MegaAppBear Avatar answered May 25 '26 03:05

MegaAppBear