WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML


 

Ionic - Cordova Native Audio


c

Using Native Audio

To have the capacity to utilize this module, we tend to first must be constrained to introduce it. Open the brief window and include the Cordova plugin.

C:\Users\Username\Desktop\MyApp>cordova module include cordova-module nativeaudio 

The following stage is to preload the sound document. There are two alternatives accessible, which are −

  • preloadSimple − It is utilized for basic sounds that will be played once.

  • preloadComplex − It is for sounds that will be played as circling sounds or foundation audio.

Add the resulting code to your controller to preload Associate in Nursing sound record. we need to verify that the Ionic stage is stacked before we will preload the sound file.

Controller Code

$ionicPlatform.ready(function() { 

$cordovaNativeAudio 

.preloadSimple('click', 'js/click.mp3') 

.then(function (msg) { 

console.log(msg); 

}, function (error) { 

console.log(error); 

}); 

$cordovaNativeAudio.preloadComplex('click', 'js/click.mp3', 1, 1) 

.then(function (msg) { 

console.log(msg); 

}, function (error) { 

console.error(error); 

}); 

});

In a similar controller, we will include code for playing sound. Our $timeout capacity will stop and empty circling sound after five seconds.

$scope.playAudio = function () { 

$cordovaNativeAudio.play('click'); 

}; 

$scope.loopAudio = function () { 

$cordovaNativeAudio.loop('click'); 

$timeout(function () { 

$cordovaNativeAudio.stop('click'); 

$cordovaNativeAudio.unload('click'); 

}, 5000); 

}

The last thing we need is to make catches for playing and circling audio.

HTML Code

<button class = "button" ng-click = "playAudio()">PLAY</button> 

<button class = "button" ng-click = "loopAudio()">LOOP</button>

When we controller on play catch, we keep an eye on territory unit going to hear the sound once and once we watch out for controller on the circle catch, the thereforeund will circle for five seconds so stop. This module works totally on partner individual or a portable device.