angular-soundmanager2
angular-soundmanager2 copied to clipboard
Error when sound finishes
Hi! First of all I'm new to soundmanager but I get this error after the track ends: Uncaught TypeError: Cannot read property 'get' of undefined angular-soundmanager2.js:6086 init.soundManager.setup.defaultOptions.onfinish angular-soundmanager2.js:3179 (anonymous function)angular-soundmanager2.js:3466 wrapCallback angular-soundmanager2.js:3178 SMSound._onfinishangular-soundmanager2.js:3801 (anonymous function) angular-soundmanager2.js:3719 (anonymous function)
The problem seems to be the autoPlay parameter that is seted to true and since I don't have anymore tracks the error arises. Is there a way to set this parameter via attribute or something like that ?
The error disappears when I create the sound via soundManager.createSound. I think I'll try to override the onfinish function in order to set the isPlaying var to false after video ends.
I have solved this by including $injector
to the ngSoundManager.factory
and changed
var angularPlayer = injector.get('angularPlayer');
to
var angularPlayer = $injector.get('angularPlayer');
(you can get rid of the lines above within the if statement)
I had this same issue - its because I was using data-ng-app
not ng-app
in my so the injector couldn't find my app.
You either need to amend [ng-app] below or change your html attribute.
onfinish: function() {
soundManager._writeDebug(this.id + ' finished playing');
if(autoPlay === true) {
//play next track if autoplay is on
//get your angular app
var elem = angular.element(document.querySelector('[ng-app]'));
//get the injector.
var injector = elem.injector();
//get the service.
var angularPlayer = injector.get('angularPlayer');
angularPlayer.nextTrack();
$rootScope.$broadcast('track:id', currentTrack);
}
}
Shouldnt this be fixed by the maintainer
@diegolz @apricot13 @melino can you please do a pull request if possible? I can't replicate this issue, so not sure what is wrong.
I've recreated the issue in plunker: http://plnkr.co/edit/hjlagTWsp4XtAFZCCaKs
to replicate - click 'play all' and wait until the first track finishes - the error is triggered when the first track finishes.
Thanks @apricot13
I see all you need to change is in your HTML code, FROM:
<html data-ng-app="myApp">
TO:
<html ng-app="myApp">
As you mentioned above, I am not sure what I can do to fix this issue for future?
This corrected my issue to with injector :+1:
Thanks @melino !
I have solved this by including $injector to the ngSoundManager.factory and changed var angularPlayer = injector.get('angularPlayer'); to var angularPlayer = $injector.get('angularPlayer'); (you can get rid of the lines above within the if statement)
@perminder-klair the quick and dirty fix would be to include support for both types, I used data- because of cross browser issues (If I remember correctly!)
var elem = angular.element(document.querySelector('[ng-app]'));
I got the same pb (using data-ng-app
instead of ng-app
)
What about using these instruction instead ?
var elem = angular.element(document.querySelector('[ng-app],[data-ng-app]'));
I tested and it seems to work.