0

I'm new to Ionic and Cordova, so I'm sure I'm missing something basic, but my problem is a packaged APK does not play sounds on an Android device. I can get the sound to play in the Ripple emulator just fine with the following code:

.controller('MainCtrl', ['$scope', function ($scope) {
        $scope.playStartBell = function () {
            var media = new Media('media/startBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        },
            $scope.playStopBell = function () {
            var media = new Media('media/stopBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        }
    }])

I've used Cordova to install the media plugin: $cordova plugin add org.apache.cordova.media

According to this SO post, a value needs to be added to the config.xml, but I'm not sure how to do it properly for Ionic/Cordova.

Community
  • 1
  • 1
binarygiant
  • 6,362
  • 10
  • 50
  • 73
  • 1
    You may want to consider creating the Media objects outside those functions so you can cache them and not re-create them on every execution. – Raymond Camden Sep 19 '14 at 11:45
  • Thanks @RaymondCamden Can you point me in the right direction for your recommendation? I was thinking of moving this code to a service that would be injected where needed. This code is just me getting my hands dirty in the framework. – binarygiant Sep 19 '14 at 14:23
  • I wasn't even thinking of a service, just literally moving the var x = new Media() lines to the beginning of the controller then access x (use a better name ;) in the functions. – Raymond Camden Sep 19 '14 at 19:19

1 Answers1

2

Turns out that you have specify path starting with the /android_asset/www prefix like so:

/android_asset/www/

So changing my code to the following worked. Note you'll want to detect what device you're running on to determine the appropriate location.

.controller('MainCtrl', ['$scope', function ($scope) {
        ///android_asset/www/
        $scope.playStartBell = function () {
            var media = new Media('/android_asset/www/media/startBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        },
            $scope.playStopBell = function () {
            var media = new Media('/android_asset/www/media/stopBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        }
    }])
binarygiant
  • 6,362
  • 10
  • 50
  • 73