ngCordova NativeAudio undefined

I’m getting the following error with NativeAudio plugin:

Uncaught TypeError:
Cannot read property ‘NativeAudio’ of undefined ng-cordova.js:2531
preloadSimple ng-cordova.js:2531
(anonymous function) app.js:23
(anonymous function) ionic.bundle.js:37388
onPlatformReady ionic.bundle.js:2241
onWindowLoad ionic.bundle.js:2220

angular.module('ngCordova.plugins.nativeAudio', [])

.factory('$cordovaNativeAudio', ['$q', '$window', function ($q, $window) {

return {
  preloadSimple: function (id, assetPath) {
    var q = $q.defer();

$window.plugins.NativeAudio.preloadSimple(id, assetPath, function (result) { // error here

      q.resolve(result);
    }, function (err) {
      q.reject(err);
    });

angular.module('starter.controllers', ['ngCordova'])

.controller('DashCtrl', function($scope, $cordovaNativeAudio, $timeout) {
	$cordovaNativeAudio
	.preloadSimple('test', 'audio/loop.mp3')
	.then(function (msg) {
		console.log(msg);
	}, function (error) {
		console.error(error);
	});
	$cordovaNativeAudio.play('test');

	$scope.play = function (url) {
		$cordovaNativeAudio.play(url, function (msg) {
			console.log(msg);
		});
	}
})

I did all the pre-requisites from www.ngcordova.com but still not working :frowning:

Have you tried to wrapping your $cordovaNativeAudio.[...] in:

document.addEventListener("deviceready", function () {
  $cordovaPlugin.someFunction().then(success, error);
}, false);

// OR with Ionic

$ionicPlatform.ready(function() {
  $cordovaPlugin.someFunction().then(success, error);
});

take a look @pbernasconi

   // Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])

.run(function($ionicPlatform) {
	$ionicPlatform.ready(function() {
		// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
		// for form inputs)
		if(window.cordova && window.cordova.plugins.Keyboard) {
			cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
		}
		if(window.StatusBar) {
			// org.apache.cordova.statusbar required
			StatusBar.styleDefault();
		}
		$cordovaNativeAudio
		.preloadSimple('test', 'audio/loop.mp3')
		.then(function (msg) {
			console.log(msg);
		}, function (error) {
			console.error(error);
		});
		$cordovaNativeAudio.play('test');
	});
})

Uncaught ReferenceError:
$cordovaNativeAudio is not defined app.js:21
(anonymous function) app.js:21
(anonymous function) ionic.bundle.js:37388
onPlatformReady ionic.bundle.js:2241
onWindowLoad ionic.bundle.js:2220

@pbernasconi Another broken plugin, just like the keychain plugin.

2 Likes

@pbernasconi Exact error message: Uncaught TypeError: Cannot read property ‘NativeAudio’ of undefined

ng-cordova.js: 1948

1 Like

I’m having the same problem, but this is how I am using the plugin:

app.controller('ludofono', function($scope, $cordovaNativeAudio, $ionicPlatform) {

$ionicPlatform.ready(function() {

	var sonidos = ['cuerdasC',
				   'cuerdasD',
				   'cuerdasE',
				   'cuerdasF',
				   'cuerdasG',
				   'cuerdasA',
				   'cuerdasB',
				   'cuerdasD',
				   'cuerdasC2',
				   'vientosB',
				   'vientosA',
				   'vientosG',
				   'vientosF',
				   'vientosE',
				   'vientosD',
				   'vientosC',
				   'percusionH',
				   'percusionL']

	for (x in sonidos) {
		$cordovaNativeAudio
	    .preloadSimple(sonidos[x], 'audio/' + sonidos[x] + '.wav')
	    .then(function (msg) {
	      console.log(msg);
	    }, function (error) {
	      alert(error);
	    });
	}	

});

$scope.tocar = function (sonido) {
	    $cordovaNativeAudio.play(sonido)
    }

$scope.$on('$destroy', function() {
  for (x in sonidos) {
	$cordovaNativeAudio.unload( sonidos[x] );
  }
})
})

And this is the error:

 TypeError: Cannot read property 'NativeAudio' of undefined
at Object.preloadSimple (http://127.0.0.1:8100/lib/ngCordova/dist/ng-cordova.min.js:8:28020)
at http://127.0.0.1:8100/controladores/ludofono.js:26:8
at http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:44815:19
at Object.ionic.Platform.ready (http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:2120:9)
at Object.IonicModule.constant.provider.$get.self.ready (http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:44813:26)
at new <anonymous> (http://127.0.0.1:8100/controladores/ludofono.js:3:17)
at invoke (http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:12884:17)
at Object.instantiate (http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:12892:27)
at http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:17161:28
at IonicModule.controller.self.appendViewElement (http://127.0.0.1:8100/lib/ionic/js/ionic.bundle.js:48253:24) <ion-nav-view class="view-container" nav-view-transition="ios">

And this is the error I get using firebug

Error: n.plugins is undefined
.preloadSimple@http://localhost:8100/lib/ngCordova/dist/ng-cordova.min.js:8:28011
@http://localhost:8100/controladores/ludofono.js:25:4
.$get</self.ready/<@http://localhost:8100/lib/ionic/js/ionic.bundle.js:44815:19
ionic.Platform.ready@http://localhost:8100/lib/ionic/js/ionic.bundle.js:2120:9
.$get</self.ready@http://localhost:8100/lib/ionic/js/ionic.bundle.js:44813:11
 @http://localhost:8100/controladores/ludofono.js:3:2
...

What am I doing wrong?

Thanks for your help :slight_smile:

Hey did any get this nativeaudio to work? I have tried it myself and keep running in the same problem. I looked around and haven’t had any luck :frowning: Any help is appreciated

1 Like

I was having this problem but when I loaded it onto an actual device it worked fine. Might be one of those plugins that doesn’t work in the browser?

Using this in the ripple emualor, which is supposed to handle cordova and still getting this bs error.

I have also face this error…plz give us proper solution…