From 68c99b32d6754562f06b1d7fc5c616fb026b9028 Mon Sep 17 00:00:00 2001 From: maflorezp Date: Fri, 3 Jun 2016 14:08:52 -0500 Subject: [PATCH] Add volume control to current track Volume it's starting in 100% (Volume value is between 0 and 1), volume sharing between all tracks Example: HTML Controller $scope.setVolume = function () { window.localStorage.setItem('volume', $scope.volume); MediaManager.setVolume($scope.volume / 100); }; --- src/factories/media-manager.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/factories/media-manager.js b/src/factories/media-manager.js index 14f715c..9be1b9e 100644 --- a/src/factories/media-manager.js +++ b/src/factories/media-manager.js @@ -1,5 +1,5 @@ angular.module('ionic-audio').factory('MediaManager', ['$interval', '$timeout', '$window', function ($interval, $timeout, $window) { - var tracks = [], currentTrack, currentMedia, playerTimer; + var tracks = [], currentTrack, currentMedia, playerTimer, volume = 1; if (!$window.cordova && !$window.Media) { console.log("ionic-audio: missing Cordova Media plugin. Have you installed the plugin? \nRun 'ionic plugin add cordova-plugin-media'"); @@ -12,6 +12,7 @@ angular.module('ionic-audio').factory('MediaManager', ['$interval', '$timeout', pause: pause, stop: stop, seekTo: seekTo, + setVolume: setVolume, destroy: destroy }; @@ -114,6 +115,7 @@ angular.module('ionic-audio').factory('MediaManager', ['$interval', '$timeout', currentMedia = createMedia(currentTrack); currentMedia.play(); + setVolume(volume); startTimer(); } @@ -124,6 +126,14 @@ angular.module('ionic-audio').factory('MediaManager', ['$interval', '$timeout', startTimer(); } + function setVolume(value) { + console.log('ionic-audio: change volume ' + value); + if (currentMedia) { + currentMedia.setVolume(value); + } + volume = value; + } + function stop() { if (currentMedia){ console.log('ionic-audio: stopping track ' + currentTrack.title); @@ -205,4 +215,4 @@ angular.module('ionic-audio').factory('MediaManager', ['$interval', '$timeout', }, 1000); } -}]); \ No newline at end of file +}]);